MATLAB Answers


Is there a function that retrieves the filename of the current script?

Is there a way to, from a .m file, determine the filename (including directory path) of that .m file?


Log in to comment.



2 Answers

Answer by Walter Roberson
on 20 Jun 2011
 Accepted Answer

S = dbstack();

You might have to use the -completenames option.

  1 Comment

Log in to comment.

Answer by Kaustubha Govind on 20 Jun 2011

   p = mfilename('fullpath')


mfilename() is right function to call. You can simply have this line in your .m file to get the file name.

mfilename does not include the extension, and thus cannot distinguish between .p and .m files.

mfilename without any option does not include the path as was specifically requested by the user.

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Win prizes and improve your MATLAB skills

Play today