Matlab

How to Create a Structure Array in MATLAB?

Structures are used to store organized data by name. They store the data in containers called fields that specific names can access. We can utilize the dot notation to assign, access, and create structure fields. A structure array is a data type used for storing the related data in the container called a field. The structure arrays are extremely useful for storing large amounts of related data and can help you efficiently as well as easily access the data you need.

Follow this guide to discover how to create a structure array in MATLAB.

How to Create a Structure Array in MATLAB?

The structure array is a data type that allows us to store related data in a container called a field. All structure fields can contain data of different data types. The data stored in the field can be accessed and created by using the dot notation. The field name can have ASCII letters (A-Z or a_z), digits (0-9), or underscores and must be started with a letter.

MATLAB allows us to create structure arrays by using the built-in struct() function.

Syntax
The struct() function uses a simple syntax to create a structure array in MATLAB, which is given below:

s = struct(field,value)
s = struct(field1,value1,...,fieldN,valueN)

Here:
The function s = struct(field,value) is used for creating a structure array having a specified field and value. The argument value can be any data type such as logical, numerical, cell array, or character.

  • If the value is a scalar cell array or not a cell array, this function returns a scalar structure.
  • If the value is the non-scalar array, this function creates a structure array having the same dimension as the value.
  • If the value is an empty cell array, this function returns an empty structure.

The function s = struct(field1, value1,…,fieldN, valueN) returns a structure array having multiple fields.

Examples
Consider some examples to understand how to create structure arrays in MATLAB.

Example 1: How to Create a Structure Array Having One Field?

This example uses the struct(field,value) function for generating a structure array in MATLAB having one field f.

field = 'f';
value = {'Linuxhint';
         [1, 2, 3];
         magic(3)};
s = struct(field,value)

We can view the field’s contents by using the dot notation:

field = 'f';
value = {'Linuxhint';
         [1, 2, 3];
         magic(3)};
s = struct(field,value);
s.f

Example 2: How to Create a Structure Array Having Multiple Fields?

The given MATLAB code creates a structure array having multiple fields using the s = struct(field1, value1,…,fieldN, valueN).

field1 = 'f1';  value1 = ones(2,5);
field2 = 'f2';  value2 = {'y', 'z'};
field3 = 'f3';  value3 = {pi/2, 2*pi};
field4 = 'f4';  value4 = {'TeamSam'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4)

We can access the field’s contents by using the dot notation:

field1 = 'f1';  value1 = ones(2,5);
field2 = 'f2';  value2 = {'y', 'z'};
field3 = 'f3';  value3 = {pi/2, 2*pi};
field4 = 'f4';  value4 = {'TeamSam'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4);
s(1).f1
s(1).f2
s(1).f3
s(1).f4

Conclusion

The structure array is an array having related data in the same field and it can be created in MATLAB using the built-in struct() function having one or multiple fields. The struct() function accepts the field and the specified value as the parameters and creates a structure array having the same dimension as the value. This tutorial provided us with an overview of creating the structure field in MATLAB with some easy examples for better understanding.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.