Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the file extension of the currently running code?

MATLAB provides the mfilename function. It returns the name of the file where the function was invoked, but unfortunately, it returns the file name without the 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 had a look at the fileparts function but it is not useful either because it only parses the string that you provide.

I am developing a piece of code has a different behavior based on the file extension. So for instance, it needs to know whether the extension of the file is .m or .p at run time.

You can check the list of extensions associated with MATLAB here.

How can I do it?

like image 450
codeaviator Avatar asked Nov 21 '17 19:11

codeaviator


1 Answers

Looking at the docs, it seems like you can get the information you need from the dbstack command, it will take some minor additional processing though.

[ST, I] = dbstack('-completenames', 1)

ST = 

    file: 'C:\myProject\myfile.m'
    name: 'myfile'
    line: 2
like image 178
Matt Clark Avatar answered Oct 19 '22 08:10

Matt Clark