MATLAB Answers

Elegantly refer to the second output from a function

493 views (last 30 days)
Xin
Xin on 9 Feb 2018
Edited: Walter Roberson on 24 Feb 2021 at 3:09
Hello everyone. I have a function (matlab function, say "ismember") and I want to use the second output from this function as index in a vector. For example:
data = rand(1,10);
[A,B] = ismember(1,[0,1,2,3,4,5]);
result = data(B);
My question is how do I skip the second line and directly use B in line 3.
Many thanks

  0 Comments

Sign in to comment.

Answers (4)

Hank
Hank on 6 Feb 2020
Edited: Hank on 6 Feb 2020
I wrote two or three functions that allow me to do this. I agree, its much nicer to be able to one line certain values from builtin functions. I hate writing
[~,~,extension] = fileparts( file );
[~,imax] = max(x);
When i want to use extension or imax directly in a statement. So put these functions in your path:
function out = out2(fun) % gets the second output argument of anonymous function fun
[~,out] = fun(); % fun must have no input arguments
end
function out = out3(fun) % gets third
[~,~,out] = fun();
end
% make a function for 4 or 5 or however many output arguments you have!
Then call these with
ext = out3(@() fileparts(file));
imax = out2(@() max(x));
Note whats happening here is that you're wrapping up your original function call in an anonymous function with no arguments. Then this gets called inside the outN function and the correct output argument is returned.
The syntax is a little more clunky, but hey, its one line, it allows something like:
switch out3(@() fileparts(file))
case '.csv'; % csv stuff
case '.txt'; % txt stuff
end

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 6 Feb 2020
See also this FEX submission, which is one function that selects any output argument/s and does not require an anonymous function (just a simple function handle):

Sign in to comment.


James Tursa
James Tursa on 9 Feb 2018
Edited: James Tursa on 9 Feb 2018
Unless the function has an input argument syntax that specifies only returning B, you can't do this. You can throw away that A immediately with this syntax however:
[~,B] = ismember(1,[0,1,2,3,4,5]);
That won't prevent the function from calculating that 1st result, it is just thrown away immediately upon return so that it doesn't clutter your workspace.

  2 Comments

Walter Roberson
Walter Roberson on 9 Feb 2018
Right. Unfortunately there is no way in MATLAB to specify that you want to extract a particular output number of an expression and use that: you have to write the outputs to variables.
James Tursa
James Tursa on 9 Feb 2018
It would be nice to have an "argout_wanted" that can be queried from within the function to find out which outputs the caller actually wants returned.

Sign in to comment.


Grigory Tagiltsev
Grigory Tagiltsev on 9 Feb 2018
You have to call the function anyway. You can do [~,B] = ismember(1,[0,1,2,3,4,5]); and avoid A. It doesn't decrease a number of lines though. You can also cheat a bit and do [B,B] = ismember(1,[0,1,2,3,4,5]); in this case only the second output will be written to B.

  0 Comments

Sign in to comment.


Han Zerui
Han Zerui on 24 Feb 2021 at 2:53
Edited: Han Zerui on 24 Feb 2021 at 2:54
Define your own ismember function:
data = rand(1,10);
result = data(myismember(1,[0,1,2,3,4,5]));
function Locb = myismember(varargin)
[~, Locb] = ismember(varargin);
end

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by