Skip to content
Related Articles

Related Articles

User Defined Data Structures in Octave
  • Last Updated : 24 Oct, 2020

Vectors and matrices are by all account not the only means that Octave offers for gathering information into a single entity. User-defined data structures are likewise accessible that empower the software engineers to make variable types that mix numbers, strings, and arrays. 

A structure is utilized to speak to data about something more confounded than a solitary number, character, or Boolean can do and more muddled than an array of the above data types can do. Structures give us an approach to “join” various kinds of data under a single variable. Interestingly, structures permit us to utilize Human Readable portrayals for our information. This is quite often a superior approach at that point to utilize a gathering of exhibits or even a phone cluster. 

For instance, let us make a data structure that contains the information for a single student. We will store the name, status (year and department), semester score (SGPA), and cumulative score (CGPA).

Code:




% Define a ‘Name’ structure to contain the name details like 
% first name, last name and middle name by putting ‘.’ operator 
% which tells Octave to use field in structure.
  
Name.First = 'Tom';
Name.MI = 'Matthew';
Name.Last = 'Curran';
Student.Name = Name;
Student.Status = 'Entc Grad';
  
% To view the contents of the structure
Student 
Student.Name
  
% Operate on the elements of structure.
Student.SGPA = 9;
Student.CGPA = 8.80;
Student
  
Student.Name.First = 'Yo';
Student.CGPA = 8.76;
Student
Student.Name
  
% To create arrays of structures.
number_students = 5;
for i = 1 : number_students
  Class(i) = Student;
end
  
Class
Class(2)
Class(2).Name
  
% Function declaration
function message = pass_or_fail(Student)
  Exam_avg = mean(Student.CGPA);
  if(Exam_avg >= 7)
    message = 'You pass!';
  else
    message = 'You fail!';
  end
  return;
end
  
% Calling ‘pass_or_fail’ function
message = pass_or_fail(Class(2)); 
  
message

Output:

Student =

  scalar structure containing the fields:

    Name =

      scalar structure containing the fields:

        First = Tom
        MI = Matthew
        Last = Curran

    Status = Entc Grad

ans =

  scalar structure containing the fields:

    First = Tom
    MI = Matthew
    Last = Curran
                                                                                                 
Student =

  scalar structure containing the fields:

    Name =

      scalar structure containing the fields:

        First = Tom
        MI = Matthew
        Last = Curran

    Status = Entc Grad
    SGPA =  9
    CGPA =  8.8000

Student =
                                                                                                 
  scalar structure containing the fields:

    Name =

      scalar structure containing the fields:

        First = Yo
        MI = Matthew
        Last = Curran

    Status = Entc Grad
    SGPA =  9
    CGPA =  8.7600

ans =

  scalar structure containing the fields:

    First = Yo
    MI = Matthew
    Last = Curran
                                                                                                 
Class =

  1x5 struct array containing the fields:

    Name
    Status
    SGPA
    CGPA

ans =

  scalar structure containing the fields:

    Name =

      scalar structure containing the fields:

        First = Yo
        MI = Matthew
        Last = Curran

    Status = Entc Grad
    SGPA =  9
    CGPA =  8.7600
                                                                                                 
ans =

  scalar structure containing the fields:

    First = Yo
    MI = Matthew
    Last = Curran

message = You pass!



My Personal Notes arrow_drop_up
Recommended Articles
Page :