154 views (last 30 days)

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) )

Matt J
on 20 Mar 2013

[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);

FM
on 28 Jan 2017

I plunked your code for vout() into a function num2csl(). I'm a bit confused by the C(:).' -- specifically why the dot. In an array, it makes any operation act on an element-by-element basis rather than on an array basis. Here, I can't see what the role is:

>> c={ 1:3 5:7 (9:12)'}

c =

[1x3 double] [1x3 double] [4x1 double]

>> c(:)

ans =

[1x3 double]

[1x3 double]

[4x1 double]

>> c(:).'

ans =

[1x3 double] [1x3 double] [4x1 double]

>> c(:)'

ans =

[1x3 double] [1x3 double] [4x1 double]

Thanks if you can clarify.

Matt J
on 28 Jan 2017

The operator .' is MATLAB's transpose operator,

It just acts here to transpose a column cell array into a row cell array.

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 6 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_137653

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_137653

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_137814

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_137814

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_137903

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_137903

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_466051

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_466051

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_466071

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_466071

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_479239

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/67874-generate-a-comma-separated-list-from-a-numeric-array#comment_479239

Sign in to comment.