Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Varargout "the other way around"?

Subject: Varargout "the other way around"?

From: Niels

Date: 1 Oct, 2013 08:51:07

Message: 1 of 3

I am trying to write a function where the number of output arguments depends on the value of one of my input arguments. However, I cannot specify upfront what the output of my function will be in a regular way.

Basically I'm looking for a way to use varargout in the script itself instead of in my function, so that I do not have to specify my output explicitly, but where I do get all the variables that are defined in my function. Putting all the output arguments in cells is not really an option, as I really need the variables explicitly named without the need to find a string in a cell.

Is this possible? :)


Small example:

runFile.m
------------
% define In1 and In2
[Out1,output] = SomeFunction(In1,In2) % the number of output arguments is variable
% do some more things
------------

SomeFunction.m
------------
function [Out1,varargout]=SomeFunction(In1,In2)
Out1 = 1;
switch In2
     case {'A' 'B'}, % ...5 output arguments...
     case {'C'}, % ...8 output arguments...
end
end
------------

Subject: Varargout "the other way around"?

From: Nasser M. Abbasi

Date: 1 Oct, 2013 09:16:19

Message: 2 of 3

On 10/1/2013 3:51 AM, Niels wrote:
> I am trying to write a function where the number of output arguments depends on
>the value of one of my input arguments. However, I cannot specify upfront what
>the output of my function will be in a regular way.
>

Just put all the return result(s) in one struct, and return one output.

--Nasser

Subject: Varargout "the other way around"?

From: matt dash

Date: 1 Oct, 2013 14:08:07

Message: 3 of 3

"Niels" wrote in message <l2e2dr$t69$1@newscl01ah.mathworks.com>...
> I am trying to write a function where the number of output arguments depends on the value of one of my input arguments. However, I cannot specify upfront what the output of my function will be in a regular way.
>
> Basically I'm looking for a way to use varargout in the script itself instead of in my function, so that I do not have to specify my output explicitly, but where I do get all the variables that are defined in my function. Putting all the output arguments in cells is not really an option, as I really need the variables explicitly named without the need to find a string in a cell.
>
> Is this possible? :)
>
>
> Small example:
>
> runFile.m
> ------------
> % define In1 and In2
> [Out1,output] = SomeFunction(In1,In2) % the number of output arguments is variable
> % do some more things
> ------------
>
> SomeFunction.m
> ------------
> function [Out1,varargout]=SomeFunction(In1,In2)
> Out1 = 1;
> switch In2
> case {'A' 'B'}, % ...5 output arguments...
> case {'C'}, % ...8 output arguments...
> end
> end
> ------------

It sound like you already know that it's possible using cell arrays. Now you just need to convince yourself that you don't need the variables to be explicitly named.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us