100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Cell Arrays and Structs

Learn MATLAB's two containers for non-uniform data: cell arrays for heterogeneous collections, and structs for named-field records, including conversions between them.

Matrices & ArraysIntermediate9 min readJul 10, 2026
Analogies

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.

matlab
% 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 contents

c(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

Was this page helpful?

Topics covered

#Programming#MATLABStudyNotes#CellArraysAndStructs#Cell#Arrays#Structs#Heterogeneous#DataStructures#StudyNotes#SkillVeris