MATLAB: Generate a comma-separated list from a numeric array

comma-separated listcslMATLAB

Is there a way to generate a comma-separated list from a numeric array without generating a temporary cell array? If not, would you find this functionality useful if it was added to MATLAB?
I know I would – the times that I've wanted to use this construct are very similar to the times that you use {:} with a cell array. For instance, you may have a set of n values in an array A that you want to pass to a function that takes varargin arguments. Right now if you had a cell array C you could do func( C{:} ), but you can't do func( A(someSyntax) ) . Similarly sometimes you want to be able to generate a CSL as an output argument list. Also, sometimes you might want to do B = {1, 2, C{:}, 'foo'}, which is different than B = {1, 2, A(:), 'foo'}. Lastly, you can't do things like a = 3; b=2; c=1; lol = [1 2 3]; [a b c] = deal( lol(someSyntax) )

Best Answer

[a b c] = deal( lol(someSyntax) )
This one you can do, and with even easier syntax
>> [a b c] = vout(lol)
a =
1
b =
2
c =
3
where vout() is defined below. You can't do it without creating a temporary cell array, but since this is hidden inside vout(), I don't see why it would matter.
function varargout=vout(C)
if isnumeric(C), C=num2cell(C); end
C=C(:).';
varargout=C(1:nargout);