[ACCEPTED]-How to elegantly ignore some return values of a MATLAB function-return-value

Accepted answer
Score: 239

With MATLAB Version 7.9 (R2009b) you can 3 use a ~, e.g.,

[~, ~, variableThatIWillUse] = myFunction();

Note that the , isn't optional. Just 2 typing [~ ~ var] will not work, and will throw an 1 error.

See the release notes for details.

Score: 40

This is somewhat of a hack, but it works:

First 8 a quick example function:

Func3 = @() deal(1,2,3);
[a,b,c]=Func3();
% yields a=1, b=2, c=3

Now the key here 7 is that if you use a variable twice on the left-hand 6 side of a multiple-expression assignment, an 5 earlier assignment is clobbered by the later 4 assignment:

[b,b,c]=Func3();
% yields b=2, c=3

[c,c,c]=Func3();
% yields c=3

(Just to check, I also verified 3 that this technique works with [mu,mu,mu]=polyfit(x,y,n) if all you 2 care about from polyfit is the third argument.)


There's 1 a better approach; see ManWithSleeve's answer instead.

Score: 37

If you wish to use a style where a variable 15 will be left to fall into the bit bucket, then 14 a reasonable alternative is

[ans, ans, variableThatIWillUse] = myfun(inputs);

ans is of course 13 the default junk variable for MATLAB, getting 12 overwritten often in the course of a session.

While 11 I do like the new trick that MATLAB now 10 allows, using a ~ to designate an ignored 9 return variable, this is a problem for backwards 8 compatibility, in that users of older releases 7 will be unable to use your code.

I generally 6 avoid using new things like that until at 5 least a few MATLAB releases have been issued 4 to ensure there will be very few users left 3 in the lurch. For example, even now I find 2 people are still using an old enough MATLAB 1 release that they cannot use anonymous functions.

Score: 13

Here's another option you can use. First 6 make a cell array to capture all the outputs 5 (you can use the NARGOUT function to determine 4 how many outputs a given function returns):

a = cell(1,3);  % For capturing 3 outputs
% OR...
a = cell(1,nargout(@func));  % For capturing all outputs from "func"

Then 3 call the function as follows:

[a{:}] = func();

Then simply 2 remove the element from a that you want, and 1 overwrite a:

a = a{3};  % Get the third output
Score: 9

I wrote a kth out function:


function kth = kthout(k, ffnc, varargin)
%% kthout: take the kth varargout from a func call %FOLDUP
%
% kth = kthout(k, ffnc, varargin)
%
% input:
%  k                      which varargout to get
%  ffnc                   function to call;
%  varargin               passed to ffnc;
% output:
%  kth                    the kth argout;
% global:
% nb:
% See also:
% todo:
% changelog:
%
%% %UNFOLD

[outargs{1:k}]    = feval(ffnc, varargin{:});
kth                             = outargs{k};

end %function

You can then 6 call

val_i_want  = kthout(3, @myfunc, func_input_1, func_input_2); %etc

You could also wrap up the function 5 like:

func_i_want = @(varargin)(kthout(3, @myfunc,varargin{:}));  % Assuming you want the third output.

After which you use

val_i_want = func_i_want(func_input_1, func_input_2);

Note that there 4 is overhead associated with using anonymous 3 functions like this, and this is not something 2 I would do in code that would be called 1 thousands of times.

Score: 4

In MATLAB 2010a, I found a neat way of doing 9 what you are asking for.

It is simply to 8 use the character "~" (without 7 the quotes of course) as your dummy variable 6 (as many as you want when returning multiple 5 parameters). This also works for input parameters 4 to functions if the functions are designed 3 to handle missing data.

I don't know if this 2 existed in previous versions, but I just 1 came across it recently.

Score: 1

You can make a function (or anonymous function) that 3 only returns selected outputs, e.g.

select = @(a,b) a(b);

Then 2 you can call your function like this:

select(func,2);
select(func,1:3);

Or 1 you can assign the output to a variable:

output(1,2:4) = select(func,1:3);
Score: 0

There isn't any reason not to use ans(n). Like 7 this:

a = rand([5 10 20 40]);

size(a);

b = ans(2);

It gives b = 10, and this way would 6 be compatible with all MATLAB versions.

Furthermore, this 5 works to get the second output argument 4 when you don't know how many arguments there 3 will be! Whereas, if you do this:

[~, b] = size(a);

Then b 2 = 8000! (You need to end with ~, to catch 1 more arguments!)

More Related questions