Tutorial

How to Check if a Variable Holds a Struct Array in MATLAB

The function isstruct determines if a variable holds a struct array.

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);

Syntax

tf = isstruct(A) determines if the variable A holds a struct array, and stores the result in the logical variable tf. An equivalent statement is tf = isa(A,'struct').

% Check if A holds a struct array
resultExample1 = isstruct(A);
% Check if A holds a struct array (isa)
resultExample2 = isa(A,'struct');
% Check if B holds a struct array
B = 'Hello World!';
resultExample3 = isstruct(B);

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 isstruct;
doc isa;

Source code

I hope you have learned how to check if a variable holds 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.