getting function name within that function run or currently running function

조회 수: 27 (최근 30일)
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
Daniel Shub
Daniel Shub 2013년 5월 31일
The mfilename function is what you probably want. If that is not working you could write a wrapper around dbstack.
Vandita
Vandita 2013년 6월 5일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Vandita
Vandita 2013년 6월 5일
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
Jan
Jan 2013년 6월 5일
편집: Jan 2013년 6월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

per isakson
per isakson 2013년 5월 31일
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 2013년 5월 31일
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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by