What Are Data Structures in MATLAB?

Data Structure in MATLAB are related to built-in data types used to organize and store related data, which can be of different types and sizes. While MATLAB important data type is the matrix, these particular structures are used for handling non-uniform, column-oriented, or hierarchical data.

The most common data structures in MATLAB are:

  • Structures (structs)
  • Cell arrays
  • Tables
  • Map containers

Data Structures

What Is a Cell Array?

A cell array is a type of data container in MATLAB that can hold different types and sizes of data within individual “cells”. While a standard matrix can only store elements of a single data type, a cell array offers flexibility by allowing each cell to contain a variable of any type, such as a number, text, or even another cell array. 

C = {1, 'EV', [3 4 5]; true, "Drive", rand(1,3)};

Accessing Cell Elements


Loop Over Cell Arrays

Looping over cell arrays in MATLAB involves iterating through each cell to access and process its contents. Since cell arrays can hold different data types in each cell, you typically use a for loop to access individual cells and their contents.

C = {'ECU1', 'ECU2', 'ECU3'};
for i = 1:length(C)
fprintf('Testing %s...\n', C{i});
end

Output:

  • Testing ECU1…
  • Testing ECU2…
  • Testing ECU3…

Structures

In MATLAB, a structure (or struct) is a data type that groups related data into a single variable using named fields. These fields can contain different types and sizes of data. Structures are particularly useful for representing objects or entities that have multiple attributes. 

car.speed = 80;
car.mode = 'sport';
car.data = [1 2 3];

Access fields:

s = car.speed; % 80

Use Cases: Storing sensor sets, calibration data, test metadata, or Simulink block configurations.


Tables

Tables are excellent for storing test results in a spreadsheet-like format.

T = table([1;2;3], [60;70;80], {'Pass';'Pass';'Fail'}, ...
'VariableNames', {'TestID', 'Speed', 'Result'});

Access columns:

T.Speed % Returns [60;70;80]

Maps (Key-Value Pairs)

Maps store data indexed by keys (e.g., message names or CAN IDs).

m = containers.Map;
m('CAN_0x100') = 'EngineSpeed';
m('CAN_0x200') = 'ThrottlePosition';

Retrieve:

m('CAN_0x100') % Returns 'EngineSpeed'

ans = ‘EngineSpeed’


Automotive Use Cases

  • Logging simulation results:
    Structures and tables organize related test data, store time stamps, sensor readings, error codes, and are used for generating and exporting reports in automotive simulations.
  • Configuring test cases:
    Cell arrays and struct arrays allow engineers to store test parameters, variants, and options flexibly, making large-scale automated testing easier.
  • CAN signal mapping:
    Maps are used to match CAN message names with IDs quickly, which is essential for decoding and processing communication across electronic control units (ECUs) in vehicles.
  • Heterogeneous output storage:
    Cell arrays can store numbers, strings, matrices, or even results from custom algorithms all in one containerโ€”perfect for catching all outputs from varied tests.
  • Automating multiple model runs:
    By keeping model file names or script references in a cell array, running batches of vehicle simulations and collecting results becomes easy and organized.