call function name in the same function

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 件のコメント

Steven Lord
Steven Lord 2024 年 4 月 20 日
Your description is quite vague. Can you post something that's a little closer to pseudocode explaining what you're trying to do?
I suspect what you're actually going to want to do is to write a function that accepts as one of its arguments "which copy" you want to run and performs the appropriate operation based on that input argument rather than making copies of the same function with minor changes. If you do use "copy and paste programming", make absolutely certain you've detected any bugs and locked down what exactly you want your function to do before making copies. Otherwise it's too easy to make changes to N-1 out of the N copies and be confused why your code doesn't do what you think it should.
Stephen23
Stephen23 2024 年 4 月 20 日
編集済み: Stephen23 2024 年 4 月 20 日
"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."
This copy-and-pasting code has a very strong https://en.wikipedia.org/wiki/Code_smell
Bruno Luong
Bruno Luong 2024 年 4 月 20 日
編集済み: Bruno Luong 2024 年 4 月 20 日
@Walter Roberson did you add an "end" to the code posted the original post?
Walter Roberson
Walter Roberson 2024 年 4 月 20 日
I'm not sure? One might have been added automatically when I formatted the code ?
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 日

1 投票

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 件のコメント

feynman feynman
feynman feynman 2024 年 4 月 21 日
thank you so much! I wonder what should be the ... in thisfun(...)? I got the following errors:
thisfun(...)
Error: This statement is incomplete.
eval(thisfun, ...)
Error: This statement is incomplete.
Walter Roberson
Walter Roberson 2024 年 4 月 21 日
Pass appropriate values into thisfun
result = thisfun(first_parameter, second_parameter, third_parameter)
Bruno Luong
Bruno Luong 2024 年 4 月 21 日
編集済み: Bruno Luong 2024 年 4 月 21 日
@feynman feynman You need to replace the '...' with whatever the input arguments when calling thisfun -whic is the handle of your principal function fun)
We don't know how you want to call your function (recursively).
Here us an example
copytoto(4)
0 1 2 3 4
function copytoto(n)
funname = dbstack().name;
thisfun = str2func(funname);
if n > 0
thisfun(n-1); % The "..." here is n-1
end
disp(n)
end
feynman feynman
feynman feynman 2024 年 4 月 21 日
Thank you all so much for the advice. This below is the function I need. But within '' functionName isn't properly recognized.
function fun
i=1
functionName=str2func(dbstack().name);
restart=uicontrol('Style','pushbutton',...
'String','restart','Callback','close;functionName');
end
Bruno Luong
Bruno Luong 2024 年 4 月 21 日
function fun
i=1 % how does it matter....
functionName=dbstack().name;
restart=uicontrol('Style','pushbutton',...
'String','restart','Callback',['close;' functionName]);
end
Bruno Luong
Bruno Luong 2024 年 4 月 21 日
編集済み: Bruno Luong 2024 年 4 月 21 日
Different approache (preferable as it does not use char array for function calling)
function fun
i=1
functionName=str2func(dbstack().name);
function MyCallback(varargin)
close;
functionName();
end
restart=uicontrol('Style','pushbutton',...
'String','restart','Callback',@MyCallback);
end
feynman feynman
feynman feynman 2024 年 4 月 21 日
@Bruno Luong thank you, but ['close;' functionName] and ['close;'functionName] report errors:
Error using horzcat
The following error occurred converting from char to function_handle:
Too many output arguments.
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
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 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by