Is there a function that retrieves the filename of the current script?
조회 수: 42 (최근 30일)
이전 댓글 표시
Is there a way to, from a .m file, determine the filename (including directory path) of that .m file?
댓글 수: 0
채택된 답변
Walter Roberson
2011년 6월 20일
S = dbstack();
S(1).file
You might have to use the -completenames option.
댓글 수: 5
Kevin Hartman
2021년 3월 13일
Additionally, S(1).name in the above example returns the filename sans extension c. MATLAB 2020b
추가 답변 (3개)
Kaustubha Govind
2011년 6월 20일
p = mfilename('fullpath')
댓글 수: 3
Walter Roberson
2011년 6월 20일
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
2019년 10월 23일
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
Rik
2022년 10월 24일
[FullPath,Filename,ext]=fileparts(matlab.desktop.editor.getActiveFilename)
Martin Koch
2022년 10월 23일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!