EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Struct
 

Matlab Struct

Priya Pedamkar
Article byPriya Pedamkar

Updated March 27, 2023

Matlab Struct

 

 

Introduction to 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

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.

Example:

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

str_stud=struct()

Output:

MATLAB STRUCT 1

2. Structure with fields

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

Output:

MATLAB STRUCT 2

3. To display values of a particular field from structure

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

Output:

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'))
str_stud.rollno(4)=4

Output:

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'))];
str_stud

Output:

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.

Example:

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.

Conclusion – Matlab Struct

A structure is a record in which each record can have information or data about various things under different fields. Matlab uses a struct to implement this record-keeping technique. Structure in Matlab can be single dimensional as well as multi-dimensional which is also called a structure array.

Recommended Articles

This has been a guide to Matlab Struct. Here we discuss the introduction, working of structure in Matlab, manipulating structures and examples. You may also have a look at the following articles to learn more –

  1. Uses Of Matlab
  2. Deep Learning Libraries
  3. Working and Examples of Matlab colorbar Label
  4. Guide to Matlab Save Variable
  5. Examples of Matlab Count
  6. fminsearch in Matlab | How to Work?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW