Programming  How to Use the Comma-Separated List

Common uses for comma-separated lists are

The following sections provide examples of using comma-separated lists with cell arrays. Each of these examples applies to MATLAB structures as well.

Constructing Arrays

You can use a comma-separated list to enter a series of elements when constructing a matrix or array. Note what happens when you insert a list of elements as opposed to adding the cell itself.

When you specify a list of elements with `C{:, 5}`, MATLAB inserts the four individual elements:

• ```A = {'Hello', C{:, 5}, magic(4)}
A =
'Hello'                    [4x4 double]
```

When you specify the `C` cell itself, MATLAB inserts the entire cell array:

• ```A = {'Hello', C, magic(4)}
A =
'Hello'    {4x6 cell}    [4x4 double]
```

Displaying Arrays

Use a list to display all or part of a structure or cell array:

• ```A{:}
ans =
Hello
ans =
34
ans =
36
ans =
38
.
.
.
```

Concatenation

Putting a comma-separated list inside square brackets extracts the specified elements from the list and concatenates them:

• ```A = [C{:, 5:6}]
A =
34    36    38    40    42    44    46    48

whos A
Name      Size                   Bytes  Class

A         1x8                       64  double array
```

Function Call Arguments

When writing the code for a function call, you enter the input arguments as a list with each argument separated by a comma. If you have these arguments stored in a structure or cell array, then you can generate all or part of the argument list from the structure or cell array instead. This can be especially useful when passing in variable numbers of arguments.

This example passes several attribute-value arguments to the `plot` function:

• ```X = -pi:pi/10:pi;
Y = tan(sin(X)) - sin(tan(X));

C{1,1} = 'LineWidth';         C{2,1} = 2;
C{1,2} = 'MarkerEdgeColor';   C{2,2} = 'k';
C{1,3} = 'MarkerFaceColor';   C{2,3} = 'g';

plot(X, Y, '--rs', C{:})
```

Function Return Values

MATLAB functions can also return more than one value to the caller. These values are returned in a list with each value separated by a comma. Instead of listing each return value, you can use a comma-separated list with a structure or cell array. This becomes more useful for those functions that have variable numbers of return values.

This example returns four values to a cell array:

• ```C = cell(1, 4);
[C{:}] = fileparts('work/mytests/strArrays.mat')
C =
'work/mytests'    'strArrays'    '.mat'     ''
``` Generating a List from a Structure Fast Fourier Transform Example © 1994-2005 The MathWorks, Inc.