[x,y] = subfuntest(a,b); When you call it with no arguments, it returns only the first, putting it in the default variable name of ans, then dumping the other arguments into the bit bucket. Answer from John D'Errico on mathworks.com
🌐
MathWorks
mathworks.com › matlabcentral › answers › 381820-elegantly-refer-to-the-second-output-from-a-function
Elegantly refer to the second output from a function - MATLAB Answers - MATLAB Central
February 9, 2018 - 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: ... https://www.mathworks.com/matlabcentral/answers/381820-elegantly-refer-to-the-second-output-from-a-function#comment_794243
🌐
MathWorks
mathworks.com › matlabcentral › answers › 365037-is-it-possible-to-obtain-only-the-second-output-from-function-file-without-assigning-a-variable-to
Is it possible to obtain only the second output from function file , without assigning a variable to the first output? - MATLAB Answers - MATLAB Central
November 4, 2017 - https://www.mathworks.com/matlabcentral/answers/365037-is-it-possible-to-obtain-only-the-second-output-from-function-file-without-assigning-a-variable-to#answer_289396 ... However, there is no way to get at this without an assignment.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 323925-cant-get-my-function-to-give-two-outputs
cant get my function to give two outputs - MATLAB Answers - MATLAB Central
February 8, 2017 - https://www.mathworks.com/matlabcentral/answers/323925-cant-get-my-function-to-give-two-outputs#answer_253832 ... amm.. I am guessing you are running the function from the Command window? try calling the function instead of:
🌐
MathWorks
mathworks.com › matlabcentral › answers › 490931-any-way-to-assign-the-second-output-to-a-variable-on-a-function
Any way to assign the second output to a variable on a function? - MATLAB Answers - MATLAB Central
November 13, 2019 - https://www.mathworks.com/matlabcentral/answers/490931-any-way-to-assign-the-second-output-to-a-variable-on-a-function ... say you have some function, function [out1, out2] = name(in1,in2).
Find elsewhere
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
nargout - Number of function output arguments - MATLAB
The minus sign indicates that the second output is varargout. The mySize function can return an indeterminate number of additional outputs. ... Function for which nargout returns the number of output arguments from its definition, specified as a function handle, a character vector, or a string ...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 362652-getting-only-one-output-from-a-function-with-several-ones
Getting only one output from a function with several ones - MATLAB Answers - MATLAB Central
October 22, 2017 - Open in MATLAB Online · Hey! I am trying to create a function that approximates the root(first output) with help of newton's method and also save every loop value(second output) in a vector called resultVec. Here's the code: function [x1, resultVec] = newt(x0) x1 = x0 - ((exp(-x0)-2.*(x0.^3)+(5.*x0))/(-exp(-x0)-6.*(x0.^2)+5)); resultVec = []; while abs(x1-x0)>=1e-10 ·
🌐
MathWorks
mathworks.com › matlabcentral › answers › 249034-why-the-second-output-doesn-t-appear
Why the second output doesn't appear? - MATLAB Answers - MATLAB Central
October 16, 2015 - https://www.mathworks.com/matlabcentral/answers/249034-why-the-second-output-doesn-t-appear#answer_196244 ... By default, MATLAB only returns the first output of a function if only one output is requested. To get both, ask for both.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 555613-how-to-handle-functions-with-multiple-outputs
How to handle functions with multiple outputs - MATLAB Answers - MATLAB Central
June 27, 2020 - In MATLAB it is not possible to capture the second or later output of a function within an expression: you need a true assignment statement and that requires a true function not an anonymous function.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1949813-output-of-two-function-results
output of two function results - MATLAB Answers - MATLAB Central
April 19, 2023 - You need to provide two output variables when you call the function. Note that the names you use when you call the function don't need to match the names that are used for the outputs inside the function.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 236351-assign-separate-handles-to-function-outputs
Assign separate handles to function outputs - MATLAB Answers - MATLAB Central
August 25, 2015 - MATLAB does not provide any mechanism to access any function output other than the first except for the case that the output is being assigned to a location with "=". You cannot access the second output of a function from within an anonymous ...
🌐
CopyProgramming
copyprogramming.com › howto › get-only-the-second-return-value-in-matlab-duplicate
Mastering MATLAB Return Values: Get Only the Second Output Effortlessly - Mastering matlab return values get only the second output effortlessly
January 28, 2026 - Functions compute all outputs regardless of requests, so ignoring extras with ~ prevents unnecessary memory allocation for unused variables. Use the tilde (~) operator to ignore unwanted outputs and capture only the second one. This is the definitive best practice since MATLAB R2009b and remains ...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 142811-minimize-second-output-of-a-function-with-respect-to-a-variable
Minimize second output of a function with respect to a variable. - MATLAB Answers - MATLAB Central
July 21, 2014 - To minimize the objective function with respect to the second output of the function “myfun”, you could write another function which calls the “myfun” and returns the second output only. ... Sign in to comment. Sign in to answer this question. Mathematics and Optimization Optimization Toolbox Solver-Based Optimization Problem Setup Choose a Solver · Find more on Choose a Solver in Help Center and File Exchange ... Find the treasures in MATLAB Central and discover how the community can help you!
Top answer
1 of 2
7

From the documentation of cellfun:

[A1,...,Am] = cellfun(func,C1,...,Cn) calls the function specified by function handle func and passes elements from cell arrays C1,...,Cn, where n is the number of inputs to function func. Output arrays A1,...,Am, where m is the number of outputs from function func, contain the combined outputs from the function calls.

So yes, cellfun can use a multi-output function and in this case it simply returns a number of outputs. If you want to use the second one only, you can use ~ to ignore the first one. The same goes for multiple outputs of anonymous functions - they will be returned if you specify multiple output arguments. Here is a simple code:

function test
    x{1} = 1;
    x{2} = 2;
    [~, B] = cellfun(@foo, x);
    f=@(c)foo(c);
    [A, B] = f(1);

    function [a b] = foo(x)
        a = x+1;
        b = x+2;
    end
end
2 of 2
2

This can be done by using a cell array as the single output of the function, instead of a function with multiple outputs.

Define your function to return a cell array (or create an auxiliary function that calls the original multiple-output function):

function F = foo2(x)
  [a,b,c] = foo(x);
  F = {a, b, c};
end

And then you can create a handle that calls your function and gets only one of the cells from the cell array.

f = @(x) cell(foo2(x)){2} % This selects the second output
g = @(x) cell(foo2(x)){3} % This selects the third output

Which is almost exactly what you were asking for. You could even create a handle that returns the n-th output

f = @(x,n) cell(foo2(x)){n}