structCreating struct array
structstruct([])struct(f1, v1, ..., fk, vk)k fields.f1, ..., fk should be character vectors containing the given field names.v1, ..., vk are given values of the fields.| MATLAB | SIMO |
|---|---|
v1, ..., vk can be a cell arrays, so that a multi-dimensional array can be created. | Cell array not supported |
| Mutli-dimensiona structure array can be created | Only scalar structure can be created |
struct is an s = struct
size(s)
s =
struct with no field
ans =
1.0000 1.0000
struct([]) is an empty structure array (with no fields).s = struct([])
size(s)
[] (struct)
ans =
0.0000 0.0000
mazda2.manufacturer = 'mazda';
mazda2.engine = struct('diesel', false, 'capacity', 1400, 'turbo', false);
mazda2.tire(1) = struct('brand', 'yokohama', 'position', 'FL');
mazda2.tire(2) = struct('brand', 'yokohama', 'position', 'FR');
mazda2.tire(3) = struct('brand', 'yokohama', 'position', 'RL');
mazda2.tire(4) = struct('brand', 'yokohama', 'position', 'RR');
corolla.manufacturer = 'toyoto';
corolla.engine = struct('disel', false, 'capacity', 1500, 'turbo', false);
corolla.tire(1) = struct('brand', 'toyo', 'position', 'FL');
corolla.tire(2) = struct('brand', 'toyo', 'position', 'FR');
corolla.tire(3) = struct('brand', 'toyo', 'position', 'RL');
corolla.tire(4) = struct('brand', 'toyo', 'position', 'RR');
model3.manufacturer = 'tesla';
model3.engine = 'electric';
model3.tire(1) = struct('brand', 'michelin', 'position', 'FL');
model3.tire(2) = struct('brand', 'michelin', 'position', 'FR');
model3.tire(3) = struct('brand', 'michelin', 'position', 'RL');
model3.tire(4) = struct('brand', 'michelin', 'position', 'RR');
prius.manufacturer = 'toyota';
prius.engine = 'hybrid';
prius.tire(1) = struct('brand', 'goodyear', 'position', 'FL');
prius.tire(2) = struct('brand', 'goodyear', 'position', 'FR');
prius.tire(3) = struct('brand', 'goodyear', 'position', 'RL');
prius.tire(4) = struct('brand', 'goodyear', 'position', 'RR');
% 2 x 2 structure array
cars = [mazda2, corolla; model3 prius]
cars =
struct array (2 x 2) with fields:
engine, tire, manufacturer