MATLAB - Structures



In MATLAB, structure arrays are a powerful data structure that allows you to organize and manipulate collections of related data using a combination of fields and indices. Structure arrays provide an efficient way to manage and process multiple instances of similar data with varying attributes, making them essential for various tasks, from data storage to complex analyses.

A structure array is an array where each element is a structure. Each structure within the array can contain multiple fields, and these fields hold different types of data, including numbers, text, logical values, and even other structures. Structure arrays are particularly useful when you have a group of similar objects with distinct characteristics.

MATLAB struct() Function

In MATLAB, the struct() function provides a convenient and versatile way to create structure arrays.

Syntax

s = struct(field,value)

The struct() function enables you to create structure arrays by specifying field names and corresponding values in a single call.

Create Structure Array

Let us see a simple example of creating a structure array using struct() function.

Example

Let us create a structured array that stores information about student names and ages.

names = {'Riya', 'Tiya', 'Siya'}
ages = [20, 22, 19]
students = struct('Name', names, 'Age', ages)

In this example, the student structure array has two fields, 'Name' and 'Age', with corresponding values assigned using cell arrays and numeric arrays.

When you execute the same in matlab command window the output is −

>> names = {'Riya', 'Tiya', 'Siya'}
ages = [20, 22, 19]
students = struct('Name', names, 'Age', ages)

names =
{
  [1,1] = Riya
  [1,2] = Tiya
  [1,3] = Siya
}

ages =

    20    22    19


students = 

  1x3 struct array with fields:

    Name
    Age

Multiple Fields in a Structure Array

Here is an example to add multiple field to a structure array.

Following is the data we will have as fields and values.

field1 = 'a1'; value1 = zeros(1,10); − This creates a field named 'a1' and assigns a numeric array of zeros with 10 elements to value1.

field2 = 'a2'; value2 = {'a', 'b'}; − This defines a field named 'a2' and assigns a cell array containing character strings 'a', 'b' to value2.

field3 = 'a3'; value3 = {pi, pi.^2}; − This declares a field named 'a3' and assigns a cell array with two elements: the mathematical constant pi and the square of pi (pi^2).

field4 = 'a4'; value4 = {'testing'}; − This establishes a field named 'a4' and assigns a cell array containing the string 'testing' to value4.

field1 = 'a1';  value1 = zeros(1,10);
field2 = 'a2';  value2 = {'a', 'b'};
field3 = 'a3';  value3 = {pi, pi.^2};
field4 = 'a4';  value4 = {'testing'};

s = struct(field1,value1,field2,value2,field3,value3,field4,value4)

When you execute the above code the resulting structure s, contains four fields: 'a1', 'a2', 'a3', and 'a4'.

>> field1 = 'a1';  value1 = zeros(1,10);
field2 = 'a2';  value2 = {'a', 'b'};
field3 = 'a3';  value3 = {pi, pi.^2};
field4 = 'a4';  value4 = {'testing'};

s = struct(field1,value1,field2,value2,field3,value3,field4,value4)

s = 

  1x2 struct array with fields:

   a1
   a2
   a3
   a4

We have cell arrays for value2 and value 2 and the size is 1-by-2 hence he size of s is 1-by-2

So s(1) and s(2) the output is as follows −

s(1)

>> s(1)

ans = 

  struct with fields:

    a1: [0 0 0 0 0 0 0 0 0 0]
    a2: 'a'
    a3: 3.1416
    a4: 'testing'

s(2)

>> s(2)

ans = 

  struct with fields:

    a1: [0 0 0 0 0 0 0 0 0 0]
    a2: 'b'
    a3: 9.8696
    a4: 'testing'

Here is a list of few built-in functions available with structures that can help you in your code when working with structures.

Function Description
fieldnames() This method will give fieldnames used in the structure
isfield() This method checks whether the given input field belongs to the structure.
isstruct() This method will tell you if the given input is a structure array or not.
rmfield() This method removes the given field from the structure array.

fieldnames()

This method will return the field names of the structure.

Syntax

fields = fieldnames(S)

Let us first create a structure and use the fieldnames() function on it.

names = {'Riya', 'Tiya', 'Siya'};
ages = [20, 22, 19];
students = struct('Name', names, 'Age', ages);
fieldnames(students)

When you execute the code in matlab the output is −

>> names = {'Riya', 'Tiya', 'Siya'};
ages = [20, 22, 19];
students = struct('Name', names, 'Age', ages);
fieldnames(students)

ans =
{
  [1,1] = Name
  [2,1] = Age
}

isfield()

The method will return 1 if the given field name is the field in the structure and 0 if it is not.

Syntax

res = isfield(S,field)

Here S is the structure array and field is the field name that you are checking if it's the field in structure array S.

Example

names = {'Riya', 'Tiya', 'Siya'};
ages = [20, 22, 19];
students = struct('Name', names, 'Age', ages);
isfield(students, 'Names')

When you test the code in matlab the output is −

>> names = {'Riya', 'Tiya', 'Siya'};
ages = [20, 22, 19];
students = struct('Name', names, 'Age', ages);
isfield(students, 'Name')

ans = 0

isstruct()

The function returns logical 1 (true) if the given input is a structure array and logical 0 (false) if not.

Syntax

a = isstruct(A)

Example

names = {'Riya', 'Tiya', 'Siya'};
ages = [20, 22, 19];
students = struct('Name', names, 'Age', ages);
isstruct(students)

When you execute the same in matlab command window.

>> names = {'Riya', 'Tiya', 'Siya'};
ages = [20, 22, 19];
students = struct('Name', names, 'Age', ages);
isstruct(students)

ans = 1

Let us give some other input then the structure array to the function isstruct().

Example

A = [1,2,3;4,5,6;7,8,9]
isstruct(A)

When you execute the above code in matlab command window the output is −

>> A = [1,2,3;4,5,6;7,8,9]
isstruct(A)

A =

   1   2   3
   4   5   6
   7   8   9

ans = 0

rmfield()

The function will remove the given field from the structure array.

Syntax

s = rmfield(S,field)

Here S is a structure array and field is the fieldname you want to remove from the structure array.

Example

field1 = 'a1';  value1 = zeros(1,10);
field2 = 'a2';  value2 = {'a', 'b'};
field3 = 'a3';  value3 = {pi, pi.^2};
field4 = 'a4';  value4 = {'testing'};

s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
t = rmfield(s, field4)

When you execute the above code in matlab the output is −

>> field1 = 'a1';  value1 = zeros(1,10);
field2 = 'a2';  value2 = {'a', 'b'};
field3 = 'a3';  value3 = {pi, pi.^2};
field4 = 'a4';  value4 = {'testing'};

s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
t = rmfield(s, field4)

s = 

  1x2 struct array with fields:

    a1
    a2
    a3
    a4


t = 

  1x2 struct array with fields:

    a1
    a2
    a3
Advertisements