Assign separate handles to function outputs

4 views (last 30 days)
Lukas Bystricky
Lukas Bystricky on 25 Aug 2015
Edited: Matt J on 25 Aug 2015
I have a function that returns two outputs, the function value and its derivative. I'd like to create separate handles to the function value and the derivative. Is this possible?
For clarity, I'd like to do something like this:
function [f, fdx] = get_values(x)
...
end
f = @(x) get_values(x) %first output
g = @(x) get_values(x) %second output somehow, not sure how to do this

Answers (1)

Matt J
Matt J on 25 Aug 2015
Edited: Matt J on 25 Aug 2015
Create an additional function
function fdx=get_fdx(x)
[~,fdx]=get_values(x);
end
and now
f = @get_values%first output
g = @get_fdx %second output somehow, not sure how to do this
  2 Comments
Matt J
Matt J on 25 Aug 2015
If you return the arguments as structure fields,
function out = get_values(x)
....
out.f=f;
out.fdx=fdx;
end
then you can do things like,
f = @(x) getfield(get_values(x),'f') %first output
g = @(x) getfield(get_values(x),'dfx') %second output

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!