📜  Octave 中的用户定义数据结构

📅  最后修改于: 2022-05-13 01:54:59.338000             🧑  作者: Mango

Octave 中的用户定义数据结构

向量和矩阵并不是 Octave 提供的将信息收集到单个实体中的唯一方式。用户定义的数据结构同样可以访问,使软件工程师能够创建混合数字、字符串和数组的变量类型。

一个结构被用来谈论一些比单个数字、字符或布尔值更混乱的数据,并且比上述数据类型的数组更混乱。结构为我们提供了一种在单个变量下“连接”各种数据的方法。有趣的是,结构允许我们利用人类可读的描绘作为我们的信息。在这一点上,这通常是利用展品聚会甚至电话集群的一种更好的方法。

例如,让我们制作一个包含单个学生信息的数据结构。我们将存储姓名、状态(年份和部门)、学期分数 (SGPA) 和累积分数 (CGPA)。

代码:

% 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

输出:

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!