Up to now, I have never come across a MATLAB function doing this directly (but maybe I'm missing something?). So, my solution would be to write a function distribute
on my own.
E.g. as follows:
result = [ 1 2 3 4 5 6 ];
[A,B,C,D,E,F] = distribute( result );
function varargout = distribute( vals )
assert( nargout <= numel( vals ), 'To many output arguments' )
varargout = arrayfun( @(X) {X}, vals(:) );
end
Explanation:
nargout
is special variable in MATLAB function calls. Its value is equal to the number of output parameters that distribute
is called with. So, the check nargout <= numel( vals )
evaluates if enough elements are given in vals
to distribute them to the output variables and raises an assertion otherwise.
arrayfun( @(X) {X}, vals(:) )
converts vals
to a cell array. The conversion is necessary as varargout
is also a special variable in MATLAB's function calls, which must be a cell array.
The special thing about varargout
is that MATLAB assigns the individual cells of varargout
to the individual output parameters, i.e. in the above call to [A,B,C,D,E,F]
as desired.
Note:
In general, I think such expanding of variables is seldom useful. MATLAB is optimized for processing of arrays, separating them to individual variables often only complicates things.
Note 2:
If result
is a cell array, i.e. result = {1,2,3,4,5,6}
, MATLAB actually allows to split its cells by [A,B,C,D,E,F] = result{:};