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

45 visualizaciones (últimos 30 días)
Is there a way to, from a .m file, determine the filename (including directory path) of that .m file?

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Jun. de 2011
S = dbstack();
S(1).file
You might have to use the -completenames option.
  5 comentarios
Kevin  Hartman
Kevin Hartman el 13 de Mzo. de 2021
Additionally, S(1).name in the above example returns the filename sans extension c. MATLAB 2020b

Iniciar sesión para comentar.

Más respuestas (3)

Kaustubha Govind
Kaustubha Govind el 20 de Jun. de 2011
p = mfilename('fullpath')
  3 comentarios
Walter Roberson
Walter Roberson el 20 de Jun. de 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.

Iniciar sesión para comentar.


Eric LePage
Eric LePage el 23 de Oct. de 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 comentarios
Rik
Rik el 24 de Oct. de 2022
@Chris Jennings You can strip the path away with the fileparts function:
[FullPath,Filename,ext]=fileparts(matlab.desktop.editor.getActiveFilename)

Iniciar sesión para comentar.


Martin Koch
Martin Koch el 23 de Oct. de 2022
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.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by