Struct Arrays in MATLAB

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

Struct arrays

Use the dot notation to specify the fields of a struct array. Each element in the array is of class struct.

% Struct array with fields 'name' and 'sex'
patients(1).name = 'Peter Pan';
patients(1).sex = 'M';

whos patients;

Struct arrays can be of any size and have the following properties:

  1. All structs in the array have the same number of fields
  2. All structs have the same field names
  3. Fields of the same name in different structs can contain different types or sizes of data
Do not overlook the third property in your code.

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 other tutorials in this series.

Source code

I hope you have understood what struct arrays are in MATLAB. The source code developed in this tutorial is available at this page.


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