From: Manthos Vogiatzoglou on
Dear all

I am having some problems using varargout.I red the documentation but it seems that I am missing something. This is my code:

varargout = myfunc(theta, data, type)

% if type = 1 the function should produce two output arguments, say v1 and v2 % while if type = 2 there are three outut arguments, v3, v4 and v5. My code is % something like that

switch type
case 1
calculate v1 and v2
case 2
calculate v3 v4 and v5
end

switch type
case 1
varargout(1) = v1; varargout(2) = v2;
case 2
varargout(1) = v3; varargout(2) = v4; varargout(3) = v5
end
%-------------------------------------------------------------------------------------- % end of code
I run the following command

[ out1 out2 out3] = myfunc(theta, data, 2)

and I get the following error:

??? In an assingment A(I) = B the number of elements in B and I must be the same

Can anyboby help me on this? Thanks in advance
From: Jan Simon on
Dear Manthos!

> I am having some problems using varargout.I red the documentation but it seems that I am missing something. This is my code:
>
> varargout = myfunc(theta, data, type)
> ...
> varargout(1) = v1; varargout(2) = v2;

1. You forgot the keyword "function" ?
function varargout = myfunc(theta, data, type)

2. VARARGOUT is a cell, so the elements have to be accessed with curly braces:
varargout{1} = v1;
varargout{2} = v2;

General advice: Use one command per line.
VARARGOUT is slow, so avoid it whenever it is possible by e.g.:
function [out1, out2] = Func(a,b,c)
switch nargout
case 2
out1 = v1;
out2 = v2;
case 1
out1 = v1;
end

Kind regards, Jan