フィルターのクリア

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

47 ビュー (過去 30 日間)
Tobin Fricke
Tobin Fricke 2011 年 6 月 20 日
コメント済み: Chris Jennings 2022 年 10 月 24 日
Is there a way to, from a .m file, determine the filename (including directory path) of that .m file?

採用された回答

Walter Roberson
Walter Roberson 2011 年 6 月 20 日
S = dbstack();
S(1).file
You might have to use the -completenames option.
  5 件のコメント
Kevin  Hartman
Kevin Hartman 2021 年 3 月 13 日
Additionally, S(1).name in the above example returns the filename sans extension c. MATLAB 2020b

サインインしてコメントする。

その他の回答 (3 件)

Kaustubha Govind
Kaustubha Govind 2011 年 6 月 20 日
p = mfilename('fullpath')
  3 件のコメント
Walter Roberson
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.
Fangjun Jiang
Fangjun Jiang 2011 年 6 月 20 日
I see. Thank you, Walter!

サインインしてコメントする。


Eric LePage
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
Rik 2022 年 10 月 24 日
@Chris Jennings You can strip the path away with the fileparts function:
[FullPath,Filename,ext]=fileparts(matlab.desktop.editor.getActiveFilename)
Chris Jennings
Chris Jennings 2022 年 10 月 24 日
perfect, thanks

サインインしてコメントする。


Martin Koch
Martin Koch 2022 年 10 月 23 日
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.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by