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?

 Risposta accettata

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

5 Commenti

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?
Sorry, nothing comes to mind.
Additionally, S(1).name in the above example returns the filename sans extension c. MATLAB 2020b

Accedi per commentare.

Più risposte (3)

p = mfilename('fullpath')

3 Commenti

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.
I see. Thank you, Walter!

Accedi per commentare.

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'

8 Commenti

Live editor creates temporary files containing just the section of code to be evaluated, and runs those. It does not execute the .mlx directly -- and in fact .mlx files are zip archives containing all of the code plus the output and various special sections for annotations and so on.
Saying which file is executing is more difficult for LiveScript.
In the special case where what you want is the name of the LiveScript, it just might be possible to use editor facilities to fetch it, perhaps.
Thanks Walter for your response. The puzzle is that I'm not using Live editor at all. I hate to admit it, but coming to terms with Live Editor seems like a large mental hurdle, the climbing of which I can postpone until after certain targets are met. Why therefore doesn't mfilename() behave well as it always has?
You are using Run Section. It does the same thing: writes the code into a temporary file and runs the temporary file. If you were to use just Run (of the whole .m file) then the mfilename would work.
Chris J
Chris J il 31 Ago 2020
Modificato: Chris J il 31 Ago 2020
Well I invested the time to jump the "large mental hurdle" and now use Live Editor pretty fluently. It's great, but AFAIK there's (still) no way to get the name of the current Live Editor filename into a variable. Very annoying!
matlab.desktop.editor.getActiveFilename
Thanks Paul, that works.
However it returns the entire path. Is there an way to return the filename only?
@Chris Jennings You can strip the path away with the fileparts function:
[FullPath,Filename,ext]=fileparts(matlab.desktop.editor.getActiveFilename)

Accedi per commentare.

With the Path class you can get the file path with
Path.this
and the parent directory with
Path.here
Use the string method to convert to a string if needed:
Path.this.string
Disclaimer: I'm the author.

Categorie

Scopri di più su Live Scripts and Functions in Centro assistenza e File Exchange

Prodotti

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by