Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

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

Asked by Tobin Fricke

Tobin Fricke (view profile)

on 20 Jun 2011

Is there a way to, from a .m file, determine the filename (including directory path) of that .m file?

0 Comments

Tobin Fricke

Tobin Fricke (view profile)

Tags

Products

2 Answers

Answer by Walter Roberson

Walter Roberson (view profile)

on 20 Jun 2011
Accepted answer
S = dbstack();
S(1).file

You might have to use the -completenames option.

1 Comment

Tobin Fricke

Tobin Fricke (view profile)

on 20 Jun 2011

Excellent, thank you.

Walter Roberson

Walter Roberson (view profile)

Answer by Kaustubha Govind

Kaustubha Govind (view profile)

on 20 Jun 2011
   p = mfilename('fullpath')

3 Comments

Fangjun Jiang

Fangjun Jiang (view profile)

on 20 Jun 2011

mfilename() is right function to call. You can simply have this line in your .m file to get the file name.
P=mfilename;

Walter Roberson

Walter Roberson (view profile)

on 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.

Fangjun Jiang

Fangjun Jiang (view profile)

on 20 Jun 2011

I see. Thank you, Walter!

Kaustubha Govind

Kaustubha Govind (view profile)

Contact us