
Other Data Structures
Now the structure is large enough that only a summary is printed:
S=
1x3 struct array with fields:
name
score
grade
There are several ways to reassemble the various fields into other MATLAB
arrays. They are mostly based on the notation of a comma-separated list.If
you type
S.score
it is the same as typing
S(1).score, S(2).score, S(3).scor e
which is a comma-separated list.
If you enclose the expression that generates such a list within square
brackets, MATLAB s tores each item from the lis t in an array. In this example,
MATLAB creates a numeric row vector containing the
score field of each
element of structure array
S:
scores = [S.score]
scores =
83 91 70
avg_score = sum(scores)/length(sco res)
avg_score =
81.3333
To create a character array from one of the text fields (name, for example), call
the
char functio n on the comma-separated list produced by S.name:
names = char(S.name)
names =
Ed Plum
Toni Miller
Jerry Garcia
4-17
Commenti su questo manuale