Is there a one-step command that identifies the name of the caller workspace

Obviously, I could use dbstack, but it seems there should be a simple command that simply prints out the name of the caller workspace.
Thanks, Leo

답변 (1개)

SK
SK 2014년 10월 13일
편집: SK 2014년 10월 13일
I think there is no option but write one yourself and put it in your library.
function name = wkname(wk)
if (length(dbstack) == 1)
name = 'base';
else
i = find(wk == filesep, 1, 'last');
if isempty(i)
i = 0;
end
name = wk(i+1:end);
end
end
This would work in the regular Matlab environment where all calls start from the command line. You could call from anywhere as:
wkname(mfilename);

댓글 수: 3

Thanks SK for this quick response. Not sure what you want for the argument mfilename? Could you explain what you had in mind please? I was thinking of a call that simply returned the name of the caller workspace, whatever it is, so there should be no need for an argument?
Presumably, your code could be modified to do that if no argument was supplied. But I'm curious as to what the argument would be used for...
mfilename is an automatic/built-in variable that always exists - it's the name of the m-file being run, but not necessarily of the function being run. If wkname() is in a different m-file, then it would probably not know the mfilename of the caller and would have to be passed in.
As per the Matlab docs, mfilename is the "file name of the most recently invoked function". So once you are in wkname(), the name of the caller is lost and it has to be passed in.
Also as Image Analyst implies, if you use wkname(mfilename) inside a local function in an m-file, it would return the file name and not the name of the local function.

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

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

태그

질문:

2014년 10월 13일

댓글:

SK
2014년 10월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by