MATLAB Answers

Andrey Kazak

Get path from running script

Asked by Andrey Kazak
on 4 Jul 2013
Latest activity Edited by Jan Simon
on 16 Apr 2015

Greetings!

I know that this is a bit hackneyed, but I couldn't get a working solution.

I run a script (not function) saved as m-file. Now I want to get path to the m-file from inside the script.

Widely suggested mfilename('fullpath') returns nothing, because it should be called from a function (but not from script).

Can you suggest a viable solution please?

Thank you!

  0 Comments

Products

No products are associated with this question.

5 Answers

Answer by the cyclist
on 4 Jul 2013
Edited by the cyclist
on 4 Jul 2013
mfilename('fullpath')

actually works for me, even inside a script. However, you could try this instead:

[pwd,'/',mfilename]

  2 Comments

the cyclist
on 4 Jul 2013

You may need to put that slash in the other direction on a PC. I forget.

Image Analyst
on 4 Jul 2013

No, you don't. Windows PCs will work just fine with slashes in either direction.


Answer by Andrey Kazak
on 4 Jul 2013

mfilename('fullpath') returns empty string on R0213a at Windows 7. [pwd,'/',mfilename] is not exactly what I need, because if your pwd is different from location of the script it will return wrong path.

  1 Comment

the cyclist
on 4 Jul 2013

This "answer" would be better placed as a comment on the relevant answer.


Answer by the cyclist
on 4 Jul 2013
Edited by the cyclist
on 4 Jul 2013

Try

which(mfilename)

This potentially has some foibles, too, if you have same-named scripts in multiple directories.

  5 Comments

the cyclist
on 4 Jul 2013

Also, I notice that

which('mfilename')   [EDITED, Jan, missing quotes added]

will return

'' not found

when I type it in the command window, but gives the result you want (I think) if you call it from inside a running script.

Andrey Kazak
on 4 Jul 2013

>> which mfilename

built-in (C:\Program Files\MATLAB\R2013a\toolbox\matlab\lang\mfilename)

But the script executes exactly as if you typed all the code of script from command window. Are you sure you're running a script, but not a function?

the cyclist
on 4 Jul 2013

Yes, I am sure I am in a script.


Answer by Image Analyst
on 4 Jul 2013

Andrey, you're getting confused between the command and the function. They behave differently so, granted, it can be confusing. When you don't put parentheses around a command's arguments, it acts like the arguments are the actual filename, not the contents of the variable with that name. When you use parentheses, it's using the function, not the command and will replace the variable name with the contents of it.

For example

>> load mymatfile

will try to load a file called mymatfile - I think it may add .mat extension though by default so it really loads mymatfile.mat. If you do

>> load(mymatfile)

it will look at the variable called mymatfile and see if it's the name of a file and try to load it. For example

>>mymatfile = 'abc.mat';
>> s = load(mymatfile);

will load abc.mat, not mymatfile.mat. The only difference is the parentheses. Note that if you didn't assign mymatfile to a string, you'd get an error.

Now look at this script, test.m:

mfilename
mfilename('fullpath') 
which(mfilename)
which('mfilename')

What do you think it will return? It returns this:

ans =
test
ans =
D:\Matlab\work\Tests\test
D:\Matlab\work\Tests\test.m
built-in (C:\Program Files\MATLAB\R2013a\toolbox\matlab\lang\mfilename)

mfilename is a function. The first time I call it, it returns the base filename - no folder and no extension. When I call "mfilename('fullpath')" it returns almost the full path - it returns the folder, base filename, but no extension.

Now, when I call which(mfilename), that is the same as issuing the command "which('test')" and that will tell MATLAB to return the full filename of the script I'm running. It's interesting to note that it returns more of the full filename than mfilename('fullpath') because it includes the extension.

Now, finally when I call which('mfilename') - with single quotes around mfilename - it considers mfilename literally as the name of an m-file that you want information on (the full path and name, including any other files with similar name in other folders on the search path). So it's really saying which('mfilename.m') which means "tell me where the function named mfilename.m lives. And since mfilename is actually a function that is contained in an m-file, it returns built-in (C:\Program Files\MATLAB\R2013a\toolbox\matlab\lang\mfilename). This is the full path and name of the m-file where the mfilename function is contained.

I hope that explains things better and makes it more understandable.

Anyone know where this command/function distinction/difference is explained in the help?

  2 Comments

Andrey Kazak
on 3 Sep 2013

I'm sorry for such a long delay. I create a script Test.m and put in the MATLAB folder in My Documents:

mfilename
mfilename('fullpath') 
which(mfilename)
which('mfilename')

and on my PC this script return the following:

ans =
     ''
ans =
     ''
'' not found.
built-in (C:\Program Files\MATLAB\R2013a\toolbox\matlab\lang\mfilename)

What do I do wrong? Why I get different results than you did?

Image Analyst
on 3 Sep 2013

I have no idea. I just did it again and got what I said, not blanks like you got.


Answer by Roberto Osorio
on 15 Apr 2015

This reply is very late, but I hope it can help users who get to this thread by searching. Experimenting with mfilename in scripts, I observed that the behavior that Andrey Kazak reports, i.e., empty strings returned from mfilename, occurs when you run the script manually (highlighting + Right-Click Evaluate or clicking Run Section in the Toolstrip). This is equivalent to typing the command in the Command Window.

To get the normal documented behavior of mfilename, you need to run the script either from a batch job or by clicking the Run button in the Toolstrip.

  2 Comments

Andrey Kazak
on 16 Apr 2015

Thank you for confirming. I create a script in R2015a and save it somewhere. Ctrl+Enter fails. Green Run button at the Toolstrip works.

Is this a bug or a feature?

Image Analyst
on 16 Apr 2015

Or this:


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today