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