Tutorial

How to Read Values From a Struct Array in MATLAB

A struct array is a data type that uses named containers called “fields” to store data of varying types and sizes.

Preparation

Create a struct array using the following code:

names = {'Peter Pan','Wendy Darling'};
sexes = {'M','F'};
ages = {11,12};
A = struct('name', names, 'sex', sexes, 'age', ages);

Single value

To read a single value, specify both the index of the target struct and the field name. If the field name is known only at runtime, use the syntax item.(exp) where exp must evaluate to a valid MATLAB identifier.

% Field 'name' of the first struct
nameItem1 = A(1).name;
% Field 'sex' of the second struct
sexItem2 = A(2).sex;
% Field 'age' of the first struct (dynamically)
field = 'age';
ageItem1 = A(1).(field);
If the index is out of range or the field does not exist, MATLAB generates an error.

Multiple values

Use the concatenation operator (i.e. []) to read a field across multiple elements. If the values are strings or have different types or sizes, use {} instead to store the result in a cell array.

% Field 'name' of the first two structs
nameItems1_2 = {A(1:2).name};
% Field 'age' of the first two structs
ageItems1_2 = [A(1:2).age];
% Field 'age' of all structs
allAges = [A.age];
% Field 'sex' of the first two structs (dynamically)
field = 'sex';
sexItems1_2 = {A(1:2).(field)};

Note: In all examples I used linear indexing to reference the target structs, but you can use subscript indexing and logical indexing too.

Further reading

I recommend the following books to learn more on struct arrays in MATLAB:

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a 2017 Textbook Excellence Award. Struct arrays, cell arrays, and other data types, are explained clearly in the eighth chapter.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Chapter 11 explains excellently struct arrays, cell arrays, and character arrays, among other data types.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

doc getfield;

Exercise

A Minimum Bounding Box (MBR) is the minimum rectangle that encloses a group of pixels. MBRs are usually defined by the location of the upper-left corner, a width, and a height.

Create a struct array using the following code. Each struct represents a bounding box.

labels = {'MBR1', 'MBR2', 'MBR3'}; 
xOrigins = {0, 10, 7};
yOrigins = {0, 5, 9};
widths = {10, 7, 3};
heights = {5, 8, 6}; 
mbrs = struct('label', labels, 'xorigin', xOrigins, 'yorigin', yOrigins, 'width', widths, 'height', heights);
  1. Store in a variable the xorigin of the first bounding box
  2. Store in a single variable the label of each bounding box
  3. Calculate the area of the second bounding box
  4. Calculate the area of each bounding box using a single statement

Source code

I hope you have learned how to read values from a struct array in MATLAB. The source code developed in this tutorial is available at this page.

Share

Your friends may need this knowledge to complete a task. Share this tutorial.