Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to elegantly ignore some return values of a MATLAB function

People also ask

How do I ignore a return value in Matlab?

To ignore function outputs in any position in the argument list, use the tilde operator. For example, ignore the first output using a tilde. [~,name,ext] = fileparts(helpFile); You can ignore any number of function outputs using the tilde operator.

How do you suppress output in Matlab?

Hear me out here, I know that to suppress output you put a semicolon at the end of a line.

What does function mean in Matlab?

A function is a group of statements that together perform a task. In MATLAB, functions are defined in separate files. The name of the file and of the function should be the same.


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

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

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

See the release notes for details.


This is somewhat of a hack, but it works:

First a quick example function:

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

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

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

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

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


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


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

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

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

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

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


Here's another option you can use. First make a cell array to capture all the outputs (you can use the NARGOUT function to determine 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 call the function as follows:

[a{:}] = func();

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

a = a{3};  % Get the third output

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;

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

end %function

You can then call

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

You could also wrap up the function 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 is overhead associated with using anonymous functions like this, and this is not something I would do in code that would be called thousands of times.


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

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

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


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

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

Then you can call your function like this:

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

Or you can assign the output to a variable:

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