getting function name within that function run or currently running function

27 ビュー (過去 30 日間)
Vandita
Vandita 2013 年 5 月 31 日
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 Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by