20

Say I have a function foo that can return three values given an input:

function [a,b,c] = foo(input)

The calculations of variables b and c take a long time, so sometimes I may wish to ignore their calculation within foo. If I want to ignore both calculations, I simply call the function like this:

output1 = foo(input);

and then include nargout within foo:

if nargout == 1
    % Code to calculate "a" only
else
    % Code to calculate other variables

The problem occurs if I want to calculate the last output, but not the second. In that case my function call would be:

[output1,~,output3] = foo(input);

Now if I use nargout within foo to check how many outputs are in the function-call, it will always return 3 because the tilde operator (~) is considered a valid output. Therefore, I cannot use nargout to determine whether or not to calculate the second output, b, within foo.

Is there any other way to do this? I.e., is it possible to check which outputs of a function-call are discarded from within the function itself?

TylerH
  • 20,799
  • 66
  • 75
  • 101
st_theory
  • 253
  • 1
  • 8
  • 2
    I can't really see an easy way to do this, other than change the ordering of your outputs, or have an additional input, which would be a flag to calculate the third output. – am304 Oct 21 '13 at 15:49
  • But no matter how I reorder my outputs there could always be a case where I want only one of either "b" or "c", and if the one I want comes after the other in the output order I will always need to use the ~ operator. A flag input seems to be the only viable way...I was hoping for something a bit more elegant that is built into Matlab, but there doesn't appear to be anything like that. – st_theory Oct 21 '13 at 16:02
  • Just dump idea. You could indicate in the input which specific one you want to compute. The usage of the function would be: output1 = foo(input); [output1,output2,output3] = foo(input);[output1,output2-3] = foo(input,'output2'); Than if nargout ==2 check the flag – freude Oct 21 '13 at 16:09
  • See also this Matlab Central thread: [How to detect when a output argument was marked unused with ~(tilde)?](http://www.mathworks.com/matlabcentral/newsreader/view_thread/325735). – horchler Oct 21 '13 at 16:42

1 Answers1

14

The commenters are basically right; this is not something that can be totally solved by the user unless The MathWorks adds functionality. However, I wrote a small function, istilde (Wayback Machine Archive), a while back that attempts to do what you ask. It works in many cases, but it's really a bit of hack and not a totally robust solution. For example, I did not attempt to get it to work for functions called from the Command Window directly (this could potentially be added with some work). Also, it relies on parsing the actual M-file, which can have issues. See the included demo file for how one might use istilde.

Feel free to edit my code for your needs – just don't use this in any production code due to the robustness issues. Any improvements would be welcome.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • I see, thanks for the link in the comment above and for this info. Hopefully MathWorks will implement its own functionality for something like this in the future! – st_theory Oct 21 '13 at 18:10
  • The link provided is broken. Can you provide a new example in this answer? – robotsfoundme Jan 26 '20 at 14:47
  • 1
    @thatrobotguy: Sorry about that. You should be able to access the files here: https://web.archive.org/web/20181008052208/http://biorobots.cwru.edu/personnel/adh/stackoverflow/04/ – horchler Feb 12 '20 at 14:57