Is there a function that retrieves the filename of the current script?
46 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tobin Fricke
am 20 Jun. 2011
Kommentiert: Chris Jennings
am 24 Okt. 2022
Is there a way to, from a .m file, determine the filename (including directory path) of that .m file?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 20 Jun. 2011
S = dbstack();
S(1).file
You might have to use the -completenames option.
5 Kommentare
Kevin Hartman
am 13 Mär. 2021
Additionally, S(1).name in the above example returns the filename sans extension c. MATLAB 2020b
Weitere Antworten (3)
Kaustubha Govind
am 20 Jun. 2011
p = mfilename('fullpath')
3 Kommentare
Walter Roberson
am 20 Jun. 2011
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.
Eric LePage
am 23 Okt. 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'
8 Kommentare
Rik
am 24 Okt. 2022
[FullPath,Filename,ext]=fileparts(matlab.desktop.editor.getActiveFilename)
Martin Koch
am 23 Okt. 2022
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!