MATLAB Answers

0

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

Asked by Tobin Fricke on 20 Jun 2011
Latest activity Commented on by Walter Roberson
on 3 Jun 2018
Is there a way to, from a .m file, determine the filename (including directory path) of that .m file?

  0 Comments

Sign in to comment.

Tags

Products

2 Answers

Answer by Walter Roberson
on 20 Jun 2011
 Accepted Answer

S = dbstack();
S(1).file
You might have to use the -completenames option.

  3 Comments

For some reason, both calling mfilename or dbstack fail when running on a cluster (Unix, using Slurm scheduler). The same code was tested on a PC (interactively) and worked fine - any ideas why this could be and what might be a way to fix this?

Sign in to comment.


Answer by Kaustubha Govind on 20 Jun 2011

p = mfilename('fullpath')

  3 Comments

mfilename() is right function to call. You can simply have this line in your .m file to get the file name.
P=mfilename;
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.

Sign in to comment.