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
Matrix Creation and Operations
Create and operate on matrices in MATLAB.
2D Plotting
Create line plots with labels, legends, and styling.
Functions and Scripts
Define functions in separate files or at end of scripts.
File I/O
Read and write .mat, .csv, and text files.
ODE Solvers
Solve ordinary differential equations with ode45.
Signal Processing (FFT)
Compute and visualize the FFT of a signal.