Maps, Tables and Other Containers
A map stores key–value pairs with fast lookup by key (like a dictionary): keys are text or numbers, values anything.
Concept
A map stores key–value pairs with fast lookup by key (like a dictionary): keys are text or numbers, values anything.
m = containers.Map() or containers.Map(keysCell, valuesCell) m('VCU') = '2.4.1', m('VCU') isKey(m, k), remove(m, k) All keys/values keys(m), values(m), m.Count
containers.Map('KeyType', 'double', 'ValueType', 'any') A map is a handle object: copying the variable doesn't copy the map (Module 28). Newer MATLAB (R2022b+) also offers dictionary.
Tables and Timetables (MATLAB) table stores column-oriented heterogeneous data with named variables — the MATLAB equivalent of a spreadsheet or data frame; timetable adds a time index. They power readtable, groupsummary, join and plotting by variable name. *MATLAB only — not run in this course* (Octave doesn't implement table in core):
Other containers: categorical (labels with a fixed set of values, MATLAB), datetime/duration (dates and times).
% MATLAB only - not run in this course
T = table({'VCU'; 'BMS'; 'GW'}, [12.6; 11.2; 12.9], 'VariableNames', {'ECU', 'Voltage'});
low = T(T.Voltage < 12, :); % filter rows
T.Status = repmat("OK", height(T), 1); % add a column
summary = groupsummary(T, 'Status', 'mean', 'Voltage');
T = readtable('bench_log.csv'); % import a CSV with headersWhy It Matters
Lookup tables (CAN signal name → ID, ECU → firmware) and tabular measurement data are daily work; maps and tables make that code clear and fast.
Syntax
ids = containers.Map({'EngineSpeed'}, {hex2dec('0CF00400')});
if isKey(ids, name), id = ids(name); endExample
16.1 containers.Map with text and numeric keys
m = containers.Map();
m('VCU') = '2.4.1';
m('BMS') = '1.9.0';
m('GW') = '3.2.0';
fprintf('Count = %d, keys = %s\n', m.Count, strjoin(keys(m), ', '))
fprintf('m(''BMS'') = %s, isKey(m, ''ABS'') = %d\n', m('BMS'), isKey(m, 'ABS'))
remove(m, 'GW');
fprintf('after remove: %s\n', strjoin(keys(m), ', '))
ids = containers.Map({'EngineSpeed', 'VehicleSpeed'}, {hex2dec('0CF00400'), hex2dec('18FEF100')});
fprintf('VehicleSpeed CAN id = 0x%08X\n', ids('VehicleSpeed'))
n = containers.Map('KeyType', 'double', 'ValueType', 'any');
n(2016) = 'VCU';
n(2017) = 'BMS';
fprintf('numeric keys: %s -> %s\n', mat2str(cell2mat(keys(n))), strjoin(values(n), ', '))Expected Result
Keys come back sorted; the numeric-key map was created by specifying KeyType.
Count = 3, keys = BMS, GW, VCU
m('BMS') = 1.9.0, isKey(m, 'ABS') = 0
after remove: BMS, VCU
VehicleSpeed CAN id = 0x18FEF100
numeric keys: [2016 2017] -> VCU, BMSCommon Mistakes
Reading a missing key (error) instead of checking isKey first.
Mixing key types in one map.
Forgetting that maps are handles (two variables share one map).
Using a struct with dynamic field names as a map for keys that aren't valid identifiers.
Best Practices
Use maps for lookups by name/ID; structs for fixed, known fields; tables for tabular data.
Wrap map access in a small function that handles missing keys.
In modern MATLAB, prefer table/timetable for measurement logs.
Interview Relevance
Medium: "how do you implement a dictionary in MATLAB?", "table vs struct vs cell?", "what is a timetable?".
Practice Questions
Count word frequencies in a sentence with a containers.Map.
Map UDS service IDs (numbers) to names and look up 0x22.
Write a function getOr(m, key, default).
(MATLAB) Load a CSV into a table and compute the mean of one column per group.
Quick Quiz
Q1. (Multiple choice) Which checks whether a key exists? A) exist(m, k) B) isKey(m, k) C) isfield(m, k) D) has(m, k) Answer: B.
Q2. (Predict the output) m.Count after adding VCU, BMS, GW and removing GW.
Answer: 2 (verified).
Q3. (True/False) Assigning m2 = m and then m2('X') = 1 also adds the key to m.
Answer: True — maps are handle objects.
Q4. (Find the bug) fw = m('ABS'); throws an error.
Answer: The key doesn't exist; check with isKey(m, 'ABS') first.
Coding Challenge
Count how many times each DTC appears in {'P0A80', 'U0100', 'P0A80', 'C1234', 'P0A80'} using a containers.Map and print the counts.
dtcs = {'P0A80', 'U0100', 'P0A80', 'C1234', 'P0A80'};
counts = containers.Map();
for k = 1:numel(dtcs)
code = dtcs{k};
if isKey(counts, code)
counts(code) = counts(code) + 1;
else
counts(code) = 1;
end
end
ks = keys(counts);
for k = 1:numel(ks)
fprintf('%s: %d\n', ks{k}, counts(ks{k}))
end