call function name in the same function

조회 수: 3 (최근 30일)
feynman feynman
feynman feynman 2024년 4월 20일
댓글: feynman feynman 2024년 4월 21일
In a function, I need to run this function somewhere within the function like
function fun
i=1
if i>1
function name
end
end
"function name" is just to call "fun", but I need to create a few copy m files of the above scripts with respective changes in the code so that the function name is actually "copy fun" or "copy 2 fun" etc and I wish "function name" to call "copy fun" or "copy 2 fun" etc.
  댓글 수: 6
feynman feynman
feynman feynman 2024년 4월 21일
hi all, thank you for your kindness but bruno's answer is what I want!
Bruno Luong
Bruno Luong 2024년 4월 21일
The "end" added by code formatting changes completely the question; whic is not doesn(t make a sense to me.

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

채택된 답변

Bruno Luong
Bruno Luong 2024년 4월 20일
편집: Bruno Luong 2024년 4월 20일
This returns the name of the function being executed
s = dbstack;
funname = s(1).name;
In recent MATLAB versions you can do in single line
funname = dbstack().name; % assign the first element of the comma list
Then you can call your recursion by
thisfun = str2func(funname);
thisfun(...); % or feval(thisfun, ...) or feval(funname, ...)
  댓글 수: 9
Bruno Luong
Bruno Luong 2024년 4월 21일
편집: Bruno Luong 2024년 4월 21일
In my first version there is NO str2fun
functionName=dbstack().name;
I later concateate only two char arrays.
You also take a freedom to change thisfun to functionname then confuse yourself with what class/type they are.
feynman feynman
feynman feynman 2024년 4월 21일
wow you are such a genius, thank you a million also to the other kind friends who helped!

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

추가 답변 (0개)

카테고리

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