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?
I'm not sure? One might have been added automatically when I formatted the code ?
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

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.
Pass appropriate values into thisfun
result = thisfun(first_parameter, second_parameter, third_parameter)
@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
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
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
@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.
wow you are such a genius, thank you a million also to the other kind friends who helped!

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

추가 답변 (0개)

카테고리

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

태그

질문:

2024년 4월 20일

댓글:

2024년 4월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by