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

New to MATLAB?

# Get path from running script

### Andrey Kazak (view profile)

on 4 Jul 2013

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!

## Products

No products are associated with this question.

### the cyclist (view profile)

on 4 Jul 2013
Edited by the cyclist

### the cyclist (view profile)

on 4 Jul 2013
mfilename('fullpath')

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

[pwd,'/',mfilename]

the cyclist

### the cyclist (view profile)

on 4 Jul 2013

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

Image Analyst

### Image Analyst (view profile)

on 4 Jul 2013

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

### Andrey Kazak (view profile)

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

### the cyclist (view profile)

on 4 Jul 2013

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

### the cyclist (view profile)

on 4 Jul 2013
Edited by the cyclist

### the cyclist (view profile)

on 4 Jul 2013

Try

which(mfilename)

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

the cyclist

### the cyclist (view profile)

on 4 Jul 2013

Also, I notice that

which(mfilename)

will return

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

### Andrey Kazak (view profile)

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

### the cyclist (view profile)

on 4 Jul 2013

Yes, I am sure I am in a script.

### Image Analyst (view profile)

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

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

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';

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?

Andrey Kazak

### Andrey Kazak (view profile)

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 =
''
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

### Image Analyst (view profile)

on 3 Sep 2013

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