Illustrations of Variable number of output arguments:
In the illustrations shown here, the user should actually know the type of the argument in order to establish how many variables to have on the left-hand side of the assignment statement. An error will answer if there are too many variables.
>> [arrtype, r,c] = typesize(4:6)
??? Error using ==> typesize
Too many output arguments.
The function nargout can be called to establish how many output arguments were used to call a function. For illustration, in the function mysize, later, a matrix is passed to the function. The functions behave like a built-in function size in that it returns the number of rows & columns. However, if three variables are used to store the answer of calling this function, then it also returns the total number of elements:
>> [r c] = mysize(eye(3))
r =
3
c =
3
>> [r c elem] = mysize(eye(3))
r =
3
c =
3
elem =
9
Note that the nargout does not return the number of output arguments in the function header, but returns the number of output arguments expected from the function (example, the number of arguments in the vector in the left-hand side of the assignment statement whenever calling the function). In a first call to the mysize function, the value of nargout be 2, therefore the function returned only the output arguments row and col. In the second call, as there were three variables on the left of the assignment statement, the value of nargout be 3, therefore the function also returned the total number of elements.