Documentation |
On this page… |
---|
What Is a Comma-Separated List? Generating a Comma-Separated List Assigning Output from a Comma-Separated List Assigning to a Comma-Separated List |
Typing in a series of numbers separated by commas gives you what is called a comma-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.
This section describes how to generate a comma-separated list from either a cell array or a MATLAB structure.
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); for k = 1:24 C{k} = k*2; end C
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}
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 through f6. Read field f5 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
You can assign any or all consecutive elements of a comma-separated list to variables with a simple assignment statement. Using the cell array C from the previous section, assign the first row to variables c1 through c6:
C = cell(4,6); for k = 1:24 C{k} = k*2; end [c1,c2,c3,c4,c5,c6] = C{1,1:6}; c5
c5 = 34
If you specify fewer output variables than the number of outputs returned by the expression, MATLAB assigns the first N outputs to those N variables, and then discards any remaining outputs. In this next example, MATLAB assigns C{1,1:3} to the variables c1, c2, and c3, and then discards C{1,4:6}:
[c1,c2,c3] = C{1,1:6};
You can assign structure outputs in the same manner:
S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2); [sf1,sf2,sf3] = S.f5; sf3
sf3 = 38
You also can use the deal function for this purpose.
The simplest way to assign multiple values to a comma-separated list is to use the deal function. This function distributes all of its input arguments to the elements of a comma-separated list.
This example uses deal to overwrite 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
Use deal to overwrite 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
Use deal to overwrite the structure fields.
[s.field1] = deal([10 20],[14 12]); s.field1
ans = 10 20 ans = 14 12
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.
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' [34] [36] [38] [40] [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]
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
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
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 = 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{:})
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'
The fftshift 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 the fftshift function:
function y = fftshift(x) numDims = ndims(x); idx = cell(1,numDims); for k = 1:numDims m = size(x,k); p = ceil(m/2); idx{k} = [p+1:m 1:p]; end y = x(idx{:}); end
The function stores the index vectors in cell array idx. Building this cell array is relatively simple. For each of the N 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 one if statement for each dimension you want the function to handle:
if ndims(x) == 1 y = x(index1); else if ndims(x) == 2 y = x(index1,index2); end end
Another way 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.