Skip to content
MATLAB

Cell Arrays and Structs

Heterogeneous data containers in MATLAB.

By EZ4Code Team
cellstructdata

Code

% Cell arrays (mixed types)
C = {'hello', 42, [1 2 3], struct('name', 'Bob')};

% Access with {} (content) vs () (cell)
C{1}     % 'hello' (string)
C(1)     % {'hello'} (1x1 cell)

% Cell array of strings
names = {'Alice', 'Bob', 'Carol'};
strcmp(names{1}, 'Alice')  % 1 (true)

% Structs
s.name = 'Alice';
s.age = 30;
s.scores = [90 85 92];

% Array of structs
people(1).name = 'Alice'; people(1).age = 30;
people(2).name = 'Bob';   people(2).age = 25;

% Struct array field access
[people.age]  % [30 25]

Explanation

Cell arrays ({}) hold mixed types — use {} for content access, () for sub-cell. Structs group named fields. Array of structs shares field names — [arr.field] concatenates a field across all elements. Use cells for variable-length strings (pre-string object era).

More MATLAB Snippets