function names as variables

15 Ansichten (letzte 30 Tage)
ahmad
ahmad am 24 Aug. 2018
Kommentiert: ahmad am 24 Aug. 2018
I have created functions (say func1 and func2) with this format (each of which was written in a separate file)
----------------------------------------
function [a,b]=func1
a=[1,1];
b=[0,0,0];
end
------------------------------------------
function [a,b]=func2
a=[5,5];
b=[1,1,0];
end
-------------------------------------
A statement like
[c,d]=func1;
works. However, it doesn't work when it is written in another function (call it mainfunc) which has func1 or func2 as arguments, i.e. briefly something like
mainfunc(x)
[c,d]=x;
where x is one of the functions func1 and func2.
Are there any suggestions to overcome this problem?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Aug. 2018
If you have
function [c, d] = mainfunc(x)
where x is being passed in as @func1 or @func2, then you would use
[c, d] = x();
With what you had, you were trying to return the function handle, rather than trying to return the result of invoking the function handle.
  1 Kommentar
ahmad
ahmad am 24 Aug. 2018
thank you so much! it solves the problem

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by