![]() |
Exploiting MATLAB's Data Structures |
db.name = 'Clay' db.height = 182creates a 1-by-1 structure array with two fields. We can add elements to the structure via indexing
db(2).name = 'Loren'at this point the db(2).height is empty since we haven't defined it yet. All undefined structure fields are initialized to empty. If you access more than one element of the structure at a time you produce a comma-separate list. Perhaps the most surprising is an expression like
db.namewhen the structure array has more than one element. In this case, we end up with the comma-separated list
db(1).name,db(2).nameThis can be used with the cell array constructor syntax to create a cell array of strings
{db.name}
Goal: Create and manipulate a structure array.
Key concepts: structures, structure fields, comma-separated lists
Key functions: struct, mean, length
db.name = 'Clay';db.height = 182;db(2).name = 'Loren';db(2).height = 120;db(3).name = 'Jack';db(3).height = 165;or
db = struct('name',{'Clay','Loren','Jack'},'height',{182 120 165});
avg = mean([db.height]);It is vectorizable.
for i=1:length(db)len(i) = length(db(i).name);endm = mean(len);It is vectorizable. Here's the alternate solution
str = char(db.name);m = length(find(isspace(str)==0))/size(str,1)
Copyright © 1997 The
MathWorks, Inc.
clay@mathworks.com
$Revision$ $Date$