Comma-Separated Lists

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

The reference material consulted while preparing these pages is listed in the section bibliography.

Written against MATLAB R2026b. The sessions below were run in GNU Octave 11.3.0 (MATLAB code that Octave also accepts) and show Octave’s output. Where an error message is quoted from MATLAB it is labelled "not run here"; Octave’s messages differ.

A comma-separated list is a sequence of values that MATLAB treats as if you had typed them separated by commas. You type 1,2,3 at the prompt and each value is returned individually. The lists that matter in practice come out of containers: the contents of several cells of a cell array, or the same field of several elements of a structure array. A list is not a value that you can store in one variable; it exists only in a place that expects several values, such as an argument list, a bracket expression or an output list. Cell arrays are on Cell Arrays and structures on Structures; functions that accept variable argument lists are on Variable Number of Arguments.

Generating a list

From a cell array. Curly-brace indexing that selects more than one cell yields their contents as a list: C{:,5} is the fifth column of a cell array, C{1,5},C{2,5},C{3,5},C{4,5}. (Parentheses, C(:,5), return a cell array, not a list.)

From a structure array. A field reference on a structure array, S.f5, is a list with one value per element: S(1).f5,S(2).f5,....

C = cell(4,6);
for k = 1:24, C{k} = k*2; end
C{:,5}
S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2);
S.f5

Octave 11.3.0 output (both statements print four values, one ans each, as the documentation page shows):

ans = 34
ans = 36
ans = 38
ans = 40
ans = 34
ans = 36
ans = 38
ans = 40

Where a list can be used

Inside square brackets: concatenation

A list inside [ ] is concatenated, which is the standard way to gather values from a cell or a structure array into a numeric array. Inside { }, it is inserted as individual cells.

B = [C{:,5:6}]
A = {'Hello', C{:,5}, magic(4)};
size(A)
vals = {S.f1}

Octave 11.3.0 output of the first two statements:

B =

   34   36   38   40   42   44   46   48

ans =

   1   6

{S.f1} is the same as {S(1).f1, S(2).f1, ...}, a cell array of the first-field values, and [S.f1] concatenates them into one array (for scalars, a numeric row). Using C itself instead of C{:,5} would insert the whole cell array as one cell, which is how the documentation distinguishes a list from a container: {'Hello',C,magic(4)} is 1-by-3, {'Hello',C{:,5},magic(4)} is 1-by-6.

As function arguments

A list becomes the input arguments of a call. This is the idiom for passing a variable number of arguments, and for forwarding name-value pairs.

args = {2,3};
zeros(args{:})
x = {1,2,3};
max(x{1:2})

Octave 11.3.0 output: a 2-by-3 matrix of zeros for zeros(args{:}), and ans = 2 for max(x{1:2}). The documentation page does the same to give plot its name-value arguments (plot(X,Y,'--rs',C{:}), where C holds 'LineWidth', 2, …​) and to build the index vectors of fftshift, with x(idx{:}) indexing an N-D array with one index per dimension and a loop-free number of subscripts.

As outputs of a call

A list on the left of =, inside brackets, collects the outputs. This works for cell arrays and for structure array fields.

[c1,c2,c3] = C{1,1:6};
c3
p = cell(1,3);
[p{:}] = fileparts('work/mytests/strArrays.mat');
p

Octave 11.3.0 output:

c3 = 18
p =
{
  [1,1] = work/mytests
  [1,2] = strArrays
  [1,3] = .mat
}

In the first statement, with fewer names than values the first three values are assigned and the others are ignored, as the documentation describes. In the second, [p{:}] = fileparts(...) receives all three outputs of fileparts into a preallocated cell array, which is the tool for calling a function with a number of outputs that is only known at run time (nargout).

deal and swapping values

deal distributes its inputs to its outputs: [a,b,c] = deal(0) copies one value to all outputs, and [a,b] = deal(1,2) assigns one to each. Combined with a list on the left, it is the way to assign to a comma-separated list:

c = cell(1,2);
[c{:}] = deal([10 20],[14 12]);
c{:}
s(1).v = 0; s(2).v = 0;
[s.v] = deal(7,8);
[s.v]
[s.v] = deal(0);
[s.v]
[a,b] = deal(1,2);
[a,b] = deal(b,a)

Octave 11.3.0 output:

ans =

   10   20

ans =

   14   12

ans =

   7   8

ans =

   0   0

a = 2
b = 1

The last statement swaps a and b: the right side is evaluated before the assignment, so deal is a readable way to swap two values of any type, with no temporary variable. The call [s.v] = deal(0) sets a field of every element of a structure array to the same value, which is the loop-free way to initialise a field. The number of inputs of deal must be 1 or equal to the number of outputs; otherwise deal raises an error (Octave 11.3.0 prints deal: nargin > 1 and nargin != nargout; MATLAB raises an error with its own text, not run here).

Using a list on the left of an assignment requires that it exists first: c{1} = []; c{2} = []; defines the elements before [c{:}] = ... is used with a colon index (the documentation notes that you cannot use list assignment with an undefined variable and : as the index).

Surprises

  • Assigning a list to one variable. q = x{:} with a three-element cell keeps only the first value and discards the rest (Octave 11.3.0 gives q = 1; MATLAB assigns the first value as well, not run here); this is not an error and hides bugs. Use q = x(:) to keep a cell array, or [q{1:3}] = x{:}.

  • An empty list. z = y{:} with an empty cell array y is an error because there is no value to assign. MATLAB reports Expected one output from a curly brace or dot indexing expression, but there were 0 results. (not run here). Octave 11.3.0 prints invalid number of elements on RHS of assignment. The same happens in a call such as f(y{:}), which passes no arguments. Guard with isempty.

  • "Too many output arguments." A list on the left needs a function that returns at least that many outputs: [a,b] = numel(x) fails because numel returns one output. A list on the right with more values than the left has names does not.

  • Using a list in the middle of an expression. C{:,5} + 1 with a list operand is an error; wrap the list in brackets ([C{:,5}] + 1) to turn it into an array.

  • Dot indexing into an empty structure array produces an empty list and the surprises above.

  • Cell versus content. C(1) is a cell; C{1} is its content; C{:} is a list.

  • A list is not displayed as one value: the Command Window shows one ans per value, so S.f5 prints four results.

Where older books differ

Books for MATLAB 5 to 7 cover deal and the varargin/varargout idiom but spend little time on the comma-separated list as a concept. The behaviour has not changed. The single-value assignment q = x{:} and the empty-list error text have changed wording between releases.