Find location of current m-file in MATLAB

mfilename or better mfilename('fullpath')


When working with classes I often like to keep associated data in the class directory. I use which to get the path and then fileparts to chop it up.

[folder, name, ext] = fileparts(which('object'));

Where 'object' can be a function or class name. The advantage of this method for me is that you can call it from outside the mfile in question. This is necessary if you need to get the path to a derived class from the base class for example.


Another method, via Walter Roberson on the MATLAB answers site, using the dbstack function:

S = dbstack('-completenames');
S(1).file