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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With