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 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.
Assigning to a Comma-Separated List
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
Comma-Separated Lists
2-81
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 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.
Assigning to a Comma-Separated List
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
Comma-Separated Lists
2-81
