MATLAB: How to get all the outputs from a function which gives random number of outputs

MATLAB

I have the following function:
function [varargout] = mytest()
r = ceil( rand * 10)
for n = 1:r
varargout{n} = n;
end
I want all the outputs from the function execution. When I try the following:
[out{1:nargout(@mytest)}]=mytest
I receive the following error message:
The left hand side is initialized and has an empty range of indices. However, the right hand side returned one or more results.
How can bring all the outputs of the function into the MATLAB workspace.

Best Answer

The ability to predetermine the number of outputs a function produces before executing the function is not available in MATLAB.
As a workaround, you can send the output as a cell array as follows:
function A = mytest
r = ceil( rand * 10)
for n = 1:r
A{n} = n;
end