Is there a function that retrieves the filename of the current script?
43 次查看(过去 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'
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!