MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
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 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

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;

2 Program Components

Free download pdf