I'm trying to use a function in another. For example, there's a defined function:
function y=super(x)
y=x.^2;
end
and I want to use it in another:
function work(super,a,b)
%blah blah blah
end
What I tried was:
f=@super
this did not work...any advice?

2 Kommentare

James Tursa
James Tursa am 12 Nov. 2020
Please show the complete code that you tried, not just snippets. Does the super( ) function reside in a file called super.m or is it part of another file? What does the f=@super have to do with how you are calling it? Etc.
SENLIN YUE
SENLIN YUE am 13 Nov. 2020
Bearbeitet: SENLIN YUE am 13 Nov. 2020
this is what I did:
function work(super,a,b)
f=@super;
if sign(f(a))~=sign(f(b))
error('a and b must be both positive or negative')
end
end
and yes, super() is a file called super.m

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

James Tursa
James Tursa am 13 Nov. 2020

0 Stimmen

So, normally one would just have f in your work function. E.g.,
function work(f,a,b)
if sign(f(a))~=sign(f(b))
error('a and b must be both positive or negative')
end
end
And then when you called the work function you would pass in a function handle. E.g.,
work(@super,a,b)

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by