Reputation: 4865
Matlab introduced for the ~
character in the list of output arguments of some routine in order to indicate we're not interested in this output value. For instance:
% Only interested for max position, not max value
[~, idx] = max(rand(1, 10));
For speed optimization reasons, is it possible from inside some routine to detect that some of the output arguments are not used ? For instance:
function [y, z] = myroutine(x)
%[
if (argout(1).NotUsed)
% Do not compute y output it is useless
y = [];
else
% Ok take time to compute y
y = timeConsummingComputations(x);
end
...
%]
Upvotes: 4
Views: 62
Reputation:
nargout
method, edited for 2nd output. Not very stable solution though, since whenever you call the function with one output argument, you need to be aware that the output is the second argument only.
function [y, z] = myroutine(x)
%[
if nargout==1
% Do not compute y output it is useless
y = [];
else
% Ok take time to compute y
y = timeConsummingComputations(x);
end
%]
Upvotes: 0
Reputation: 4191
It may not be the best one but an easy solution is to add another input argument
function [y, z] = myroutine(x, doYouWantY)
%[
if doYouWantY == 0
% Do not compute y output it is useless
y = [];
else
% Ok take time to compute y
y = timeConsummingComputations(x);
end
...
%]
Upvotes: 2