getting function name within that function run or currently running function

29 views (last 30 days)
I wish to save my output files in the name of the function that is producing those results. As I change versions of functions and their names very frequently, and forget changing output folder name each time. This makes it very difficult to trace the function versions that produced those results. If I can somehow get the function name automatically inside the function itself using some function and pass that string to output folder name that would help me solve my issue. Is there any function which can be used for this? I saw mfilename function (<http://www.mathworks.in/help/matlab/ref/mfilename.html>) but didn't understand how to use it. I got a blank string on using mfilename. Please see if anyone can help.
  2 Comments
Daniel Shub
Daniel Shub on 31 May 2013
The mfilename function is what you probably want. If that is not working you could write a wrapper around dbstack.
Vandita
Vandita on 5 Jun 2013
I could do it simply with mfilename function. Here is the solution to saving folder names during a run:
folder_name = mfilename('give here path where function file resides'); % to get the function name with currently running function
eval(['mkdir ', folder_name]); %to create a folder with the name of the fucntion

Sign in to comment.

Accepted Answer

Vandita
Vandita on 5 Jun 2013
Sorry for trouble, I could do it myself with mfilename function.
Here is the solution to saving folder names for outputs during a run, without predefining it(i.e. defining it during a run):
folder_name = mfilename('give here path where function file resides'); % to get the function name with currently running function
eval(['mkdir ', folder_name]); %to create a folder with the name of the fucntion
  1 Comment
Jan
Jan on 5 Jun 2013
Edited: Jan on 5 Jun 2013
I do not understand your solution. mfilename does not accept a "path where the function resides" as input, but I assume you mean
folder_name = fileparts(mfilename('fullpath'));
Starting the mkdir command through EVAL is an ugly and susceptible construction. Better run the command directly and avoid EVAL completely:
mkdir(folder_name)
Avoiding EVAL is a good idea in general, because there is always a better solution (except for 2 or three very rare and strange exceptions, which have been found by some Matlab cracks in this forum).
But finally there is no reason to create this folder, because it must be existing already - otherwise it could not be the parentfolder of the current M-file.

Sign in to comment.

More Answers (2)

per isakson
per isakson on 31 May 2013
Try
>> cssm
which returns
ans =
cssm/cssm2
where
function name = cssm()
name = cssm1();
function name = cssm1()
name = cssm2();
end
function name = cssm2()
name = current_function('-full');
end
end
and where
function caller_name = current_function( inarg )
%
% See also: mfilename
dbk = dbstack( 1 );
if isempty( dbk )
str = 'base';
else
str = dbk(1).name;
end
ixf = find( str == '.', 1, 'first' );
if isempty( ixf ) || ( nargin==1 && strcmp( inarg, '-full' ) )
caller_name = str;
else
caller_name = str( ixf+1 : end );
end
end

Kaustubha Govind
Kaustubha Govind on 31 May 2013
mfilename seems like the right solution for your case. Are you testing it by calling it from the MATLAB command window by any chance? You need to call it from a function. For example:
function mytest
fprintf('Currently executing %s\n', mfilename);
end
>> mytest
Currently executing mytest

Products

Community Treasure Hunt

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

Start Hunting!