Cell Arrays: Heterogeneous Containers
Unlike a numeric matrix, whose every element must share the same type and size, a cell array stores arbitrary, differently-typed and differently-sized content in each slot, created with curly braces: c = {1, 'hello', [1 2 3]} holds a number, a string, and a vector in one container. Accessing content requires curly-brace indexing c{1} to unwrap the actual contents, whereas parenthesis indexing c(1) returns a 1x1 cell wrapping that same content, a distinction that trips up many beginners.
Cricket analogy: A cell array like {1, 'hello', [1 2 3]} is like a player's kit bag holding a bat, a cap, and a set of pads together, all different types of items in one container, unlike a scorecard's rigid rows of same-type numbers.
Structs: Named Fields
A struct groups related data under named fields rather than numeric positions: s.name = 'Widget'; s.price = 19.99; s.inStock = true; builds a struct with three fields accessed by dot notation. Struct arrays extend this to collections of records, s(1).name = 'A'; s(2).name = 'B';, and fieldnames(s) lists all field names programmatically, while isfield(s,'price') checks whether a field exists.
Cricket analogy: s.name='Kohli'; s.runs=254; building a struct is like a player's individual stat card with named fields for name and runs, and a struct array s(1), s(2) mirrors an entire team roster where every player has the same named fields but different values.
Converting Between Cells and Structs
struct2cell(s) and cell2struct convert between the two representations, and cellfun/arrayfun apply a function across every element of a cell array or struct array respectively without an explicit loop, for example cellfun(@length, c) returning the length of each cell's contents. num2cell and the comma-separated-list expansion c{:} (spreading every cell as separate function arguments) are common idioms when interfacing cell data with functions expecting individual arguments.
Cricket analogy: cellfun(@length, c) computing the length of each cell's contents in one pass mirrors quickly tallying the number of boundaries hit by each batsman across an entire lineup without checking scorecards one at a time.
% Cell array: heterogeneous container
c = {1, 'hello', [1 2 3]};
disp(c{2}); % 'hello' -- curly braces unwrap contents
wrapped = c(2); % 1x1 cell still containing 'hello'
% Struct: named fields
s.name = 'Widget';
s.price = 19.99;
s.inStock = true;
% Struct array
inv(1).name = 'A'; inv(1).price = 5;
inv(2).name = 'B'; inv(2).price = 8;
names = {inv.name}; % comma-separated list -> cell array
fn = fieldnames(s); % {'name'; 'price'; 'inStock'}
hasPrice = isfield(s, 'price'); % true
lens = cellfun(@length, c); % length of each cell's contentsc(1) and c{1} are different: c(1) returns a 1x1 cell array (still wrapped), while c{1} returns the unwrapped content itself. Using the wrong one is the single most common cell-array bug for MATLAB beginners.
Growing a struct array or cell array one element at a time inside a loop (e.g., s(end+1) = newEntry) is expensive because MATLAB may need to reallocate memory on every iteration — for known sizes, preallocate with cell(1,n) or struct('field',cell(1,n)) before the loop.
- Cell arrays {} store heterogeneous, differently-sized data; curly braces c{i} unwrap content, parentheses c(i) keep it wrapped.
- Structs group related data under named fields accessed with dot notation, e.g., s.name.
- Struct arrays (s(1), s(2), ...) let every element share the same field names with different values.
- fieldnames(s) lists field names; isfield(s,'field') checks existence.
- cellfun and arrayfun apply a function across every element of a cell array or struct array without an explicit loop.
- struct2cell and cell2struct convert between the two container types.
- Preallocate cell/struct arrays before a loop to avoid costly repeated reallocation.
Practice what you learned
1. What is the difference between c(1) and c{1} for a cell array c?
2. How do you access the 'price' field of struct s?
3. What does cellfun(@length, c) do?
4. Why should you preallocate a struct array before a loop that fills it?
5. What can a single cell array store that a numeric matrix cannot?
Was this page helpful?
You May Also Like
Matrices and Vectors in MATLAB
Learn how MATLAB represents scalars, vectors, and matrices as a single unified array type, and how to construct them with literals and built-in functions.
Indexing and Slicing in MATLAB
Master subscript and linear indexing, colon-based slicing, the end keyword, and logical indexing to access and filter matrix data efficiently.
Vectorization and Broadcasting
Learn to replace explicit loops with whole-array operations and use MATLAB's implicit expansion (broadcasting) rules for faster, more concise code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics