Assign separate handles to function outputs
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Matt J
2015-8-25
编辑:Matt J
2015-8-25
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 个评论
Walter Roberson
2015-8-25
I agree. Notice that you need a real function for this, not an anonymous function. MATLAB does not provide any mechanism to access any function output other than the first except for the case that the output is being assigned to a location with "=". You cannot access the second output of a function from within an anonymous function.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!