[TOC]

struct

Creating struct array

Usage

struct

struct([])

struct(f1, v1, ..., fk, vk)

Difference with MATLAB

MATLABSIMO
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 createdOnly scalar structure can be created

Examples

Input
s = struct
size(s)
Output
s =
 struct with no field

ans = 
 1.0000   1.0000
Input
s = struct([])
size(s)
Output
[] (struct)

ans = 
 0.0000   0.0000
Input
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]
Output
cars =
 struct array (2 x 2) with fields:
 engine, tire, manufacturer