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

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

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jun 2011
S = dbstack();
S(1).file
You might have to use the -completenames option.
  5 Comments

Sign in to comment.

More Answers (2)

Kaustubha Govind
Kaustubha Govind on 20 Jun 2011
p = mfilename('fullpath')
  3 Comments

Sign in to comment.


Eric LePage
Eric LePage on 23 Oct 2019
I'd appreciate a qualification, because what I get for R2019b is not very helpful; it certainly isn't the name of the currently running mfile. Thanks.
P = mfilename
P =
'LiveEditorEvaluationHelperESectionEval67edf31d'
or
S = dbstack();
>> S(1).file
ans =
'LiveEditorEvaluationHelperESectionEval67edf31d.mlx'
  5 Comments

Sign in to comment.

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!