call function name in the same function
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
feynman feynman
il 20 Apr 2024
Commentato: feynman feynman
il 21 Apr 2024
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 Commenti
Bruno Luong
il 21 Apr 2024
The "end" added by code formatting changes completely the question; whic is not doesn(t make a sense to me.
Risposta accettata
Bruno Luong
il 20 Apr 2024
Modificato: Bruno Luong
il 20 Apr 2024
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 Commenti
Bruno Luong
il 21 Apr 2024
Modificato: Bruno Luong
il 21 Apr 2024
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!