Tutorial

How to Write Values to 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 write 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.

% Set the field 'name' of the first struct
A(1).name = 'Peter P.';
% Set the field 'age' of the first struct (dynamically)
field = 'age';
A(1).(field) = 10;

If the index is out of range, MATLAB expands the struct array as required. If the field does not exist, MATLAB creates it. In both cases, unassigned fields are initialized to an empty array.

Multiple values

To write a field across multiple elements, use the concatenation operator (i.e. []) in the left part of the assignment, and generate a comma-separated list in the right part. To set a field to the same value across all target elements, use the deal function.

% Set the field 'name' of the first two structs
newNames = {'Peter','Wendy'};
[A(1:2).name] = newNames{:};
% Set the field 'age' of the first two structs (dynamically)
field = 'age';
newAges = {12,13};
[A(1:2).(field)] = newAges{:};
% Set the field 'sex' of the first two structs
[A(1:2).sex] = deal('');
% Set the field 'age' of all structs
[A.age] = deal(11);

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 pages in the MATLAB help documentation, and the other tutorials in this series.

doc setfield;
doc deal;

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. Set the width of the first bounding box to 9
  2. Set the height of the first two bounding boxes to 4 and 5, respectively
  3. Set the xorigin of the last two bounding boxes to 0
  4. Set the label of each bounding box to the string ‘Box ID’

Note: Use only one code statement per clause.

Source code

I hope you have learned how to write values to 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.