Comma-Separated Lists
What Is a Comma-Separated List?
Typing in a series of numbers separated by commas gives you what is called acomma-separated list. The MATLAB®software returns each value individually:
1,2,3
ans = 1 ans = 2 ans = 3
Such a list, by itself, is not very useful. But when used with large and more complex data structures like MATLAB structures and cell arrays, the comma-separated list can enable you to simplify your MATLAB code.
Generating a Comma-Separated List
This section describes how to generate a comma-separated list from either a cell array or a MATLAB structure.
Generating a List from a Cell Array
Extracting multiple elements from a cell array yields a comma-separated list. Given a 4-by-6 cell array as shown here
C = cell(4,6);fork = 1:24 C{k} = k*2;endC
C = [2] [10] [18] [26] [34] [42] [4] [12] [20] [28] [36] [44] [6] [14] [22] [30] [38] [46] [8] [16] [24] [32] [40] [48]
extracting the fifth column generates the following comma-separated list:
C{:,5}
ans = 34 ans = 36 ans = 38 ans = 40
This is the same as explicitly typing
C{1,5},C{2,5},C{3,5},C{4,5}
Generating a List from a Structure
For structures, extracting a field of the structure that exists across one of its dimensions yields a comma-separated list.
Start by converting the cell array used above into a 4-by-1 MATLAB structure with six fields:f1
throughf6
. Read fieldf5
for all rows and MATLAB returns a comma-separated list:
S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2); S.f5
ans = 34 ans = 36 ans = 38 ans = 40
This is the same as explicitly typing
S(1).f5,S(2).f5,S(3).f5,S(4).f5
Assigning Output from a Comma-Separated List
You can assign any or all consecutive elements of a comma-separated list to variables with a simple assignment statement. Using the cell arrayC
from the previous section, assign the first row to variablesc1
throughc6
:
C = cell(4,6);fork = 1:24 C{k} = k*2;end[c1,c2,c3,c4,c5,c6] = C{1,1:6}; c5
c5 = 34
C{1,1:3}
to the variablesc1
,c2
, andc3
, and then discardsC{1,4:6}
:[c1,c2,c3] = C{1,1:6};
S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2); [sf1,sf2,sf3] = S.f5; sf3
sf3 = 38
deal
function for this purpose.
Assigning to a Comma-Separated List
The simplest way to assign multiple values to a comma-separated list is to use thedeal
function. This function distributes all of its input arguments to the elements of a comma-separated list.
This example usesdeal
覆盖each element in a comma-separated list. First create a list.
c{1} = [31 07]; c{2} = [03 78]; c{:}
ans = 31 7 ans = 3 78
Usedeal
覆盖each element in the list.
[c{:}] = deal([10 20],[14 12]); c{:}
ans = 10 20 ans = 14 12
This example does the same as the one above, but with a comma-separated list of vectors in a structure field:
s(1).field1 = [31 07]; s(2).field1 = [03 78]; s.field1
ans = 31 7 ans = 3 78
Usedeal
to overwrite the structure fields.
[s.field1] = deal([10 20],[14 12]); s.field1
ans = 10 20 ans = 14 12
How to Use the Comma-Separated Lists
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 alistof elements as opposed to adding the cell itself.
When you specify a list of elements withC{:, 5}
, MATLAB inserts the four individual elements:
A = {'Hello',C{:,5},magic(4)}
A = 'Hello' [34] [36] [38] [40] [4x4 double]
When you specify theC
cell itself, MATLAB inserts the entire cell array:
A = {'Hello',C,magic(4)}
一个= '你好' {4 x6细胞}[4 x4的两倍)
Displaying Arrays
Use a list to display all or part of a structure or cell array:
A{:}
ans = Hello ans = [2] [10] [18] [26] [34] [42] [4] [12] [20] [28] [36] [44] [6] [14] [22] [30] [38] [46] [8] [16] [24] [32] [40] [48] ans = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
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
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 theplot
function:
X = -pi:pi/10:pi; Y = tan(sin(X)) - sin(tan(X)); C = cell(2,3); C{1,1} ='LineWidth'; C{2,1} = 2; C{1,2} ='MarkerEdgeColor'; C{2,2} ='k'; C{1,3} ='MarkerFaceColor'; C{2,3} ='g'; figure 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 three values to a cell array:
C = cell(1,3); [C{:}] = fileparts('work/mytests/strArrays.mat')
C = 'work/mytests' 'strArrays' '.mat'
Fast Fourier Transform Example
Thefftshift
function swaps the left and right halves of each dimension of an array. For a simple vector such as[0 2 4 6 8 10]
the output would be[6 8 10 0 2 4]
. For a multidimensional array,fftshift
performs this swap along each dimension.
fftshift
uses vectors of indices to perform the swap. For the vector shown above, the index[1 2 3 4 5 6]
is rearranged to form a new index[4 5 6 1 2 3]
. The function then uses this index vector to reposition the elements. For a multidimensional array,fftshift
must construct an index vector for each dimension. A comma-separated list makes this task much simpler.
Here is thefftshift
function:
functiony = fftshift(x) numDims = ndims(x); idx = cell(1,numDims);fork = 1:numDims m = size(x,k); p = ceil(m/2); idx{k} = [p+1:m 1:p];endy = x(idx{:});end
The function stores the index vectors in cell arrayidx
. Building this cell array is relatively simple. For each of theN
dimensions, determine the size of that dimension and find the integer index nearest the midpoint. Then, construct a vector that swaps the two halves of that dimension.
By using a cell array to store the index vectors and a comma-separated list for the indexing operation,fftshift
shifts arrays of any dimension using just a single operation:y = x(idx{:})
. If you were to use explicit indexing, you would need to write oneif
statement for each dimension you want the function to handle:
ifndims(x) == 1 y = x(index1);其他的ifndims(x) == 2 y = x(index1,index2);endend
另一种方式to handle this without a comma-separated list would be to loop over each dimension, converting one dimension at a time and moving data each time. With a comma-separated list, you move the data just once. A comma-separated list makes it very easy to generalize the swapping operation to an arbitrary number of dimensions.