2. After the Advance

Matlab Struct

A structure is defined as the record-making process having various fields with different names. All fields can have different types of data whereas a single field should have some type of data. The keyword used for a structure in Matlab is “struct” Array of a structure is also possible in Matlab. A struct can have a single field, many fields, and even no field. It can be one dimensional or multi-dimensional. Value to the structure can be added using a structure name and filedname connected with the dot operator. Character values should be added using ‘ ‘ whereas numerical value can be added as it is.


str_name = struct('fieldname1', value1, 'fieldname2', value2, ...)

This creates a structure array with the specified fields and values. The type of value arrays i.e. value1, value2, etc., should be cell having the same size. The field name of the structure field is case-sensitive and should start with an alphabet. The remaining part of the field name can have alphabets, numbers, and special characters. Command “namelengthmax” can be used to find the maximum length of a field name.

Following are various methods to create a structure:

  • str_name = struct(‘fieldname1’, {}, ‘fieldname2’, {}, …): Creates a structure which is empty with fields fieldname1, fieldname2, …
  • struct([]): Creates a structure which is empty and has no fields.
  • struct(o): Transforms the object o into its equivalent structure.


As an example, if we want to create a record of students in a class.

Using struct it can be done as follows:

str_stud=struct(‘rollno’,[1 2 3], ‘subjects’, char(‘physics’, ‘chemistry’, ‘maths’))

This example has a structure name as str_stud with field names rollno and subjects. Each having different values.

Working of Structure in Matlab

Given below is the working of structure in Matlab:

1. Structure with no field




2. Structure with fields

str_stud=struct('rollno',[1 2 3],'subjects',char('phy', 'chem' ,'maths'))



3. To display values of a particular field from structure

str_stud=struct('rollno',[1 2 3],'subjects',char('phy', 'chem' ,'maths'))


values from particular field from structure

4. To add new value in the field

str_stud=struct('rollno',[1 2 3],'subjects',char('phy', 'chem' ,'maths'))


add new value in field

5. Example of structure array

str_stud=[struct(‘rollno’,[1 2 3], 'subjects',char('phy', 'chem' ,'maths'));
struct(‘rollno’,[4 5 6], 'subjects',char('phy', 'chem' ,'bio'));
struct(‘rollno’,[7 8 9], 'subjects',char('maths', 'comp' ,’sst'))];


This will create a 3X1 structure array name str_stud.

matlab struct 5

Manipulating Structures

Structure manipulation is similar to the manipulation of arrays i.e by accessing elements of the structure using indexing and change the values of particular fields. The main difference is that all values of a field across a structure array cannot be assigned to a variable using a colon range specifier.


When str_stud is a 3X1 structure array, the following are the valid and non-valid operations in structures:

  • Str_stud(1).subjects(2) is valid and gives the second element of subjects from the first record of str_stud
  • Str_stud(1).subjects(:) gives all elements of subjects from Str_stud(1), same as Str_stud(1).subjects
  • A=Str_stud(:).subjects is invalid, does not assign subjects from all records to A, though the command Str_stud(:).subjects or Str_stud.subjects displays subjects from all records.

To assign values to a variable, we have to use a loop construction:

  • For i=1:3, all_subjects(i,:)= Str_stud(i).subjects; end.

The values cannot be assigned directly using a colon as the values of fields from various records are considered separate entities. The field contents can also be of different sizes. Hence, although the loop can be used for assignment, yet assignment should make sense. For example, in the for loop mention in the code above, Str_stud(2).subjects has only 2 entries, then the assignment will give an error. So, it is proved that indices can be used for structures and also fields for accessing data. Here, we have shown a small example. However, we can also have a structure inside structures in which the level of indexing will be a bit tough and confusing, and require extra effort in case of nested structures.

Leave a Reply

Your email address will not be published. Required fields are marked *