How to find the file extension of the current code?

MATLAB provides the mfilename function. It returns the name of the file in which the function was called, but, unfortunately, it returns the name of the file without extension .

So, for example, if we have a file called myfile.m , and we call mfilename inside the file, it will return the string 'myfile' , but not 'myfile.m'

I also looked at the fileparts function, but this is not useful either because it only parses the string you provide.

I am developing a piece of code that has different behavior based on the file extension. So, for example, he needs to know if the file extension is .m or .p at runtime .

You can check the list of extensions related to MATLAB here .

How can i do this?

+8
reflection matlab file-extension
source share
1 answer

Looking at the docs , it looks like you can get the information you need from the dbstack , a bit of extra processing is required.

 [ST, I] = dbstack('-completenames', 1) ST = file: 'C:\myProject\myfile.m' name: 'myfile' line: 2 
+9
source share

All Articles