Октавная структура внутри структуры - PullRequest
0 голосов
/ 02 октября 2019

Я хочу создать структуру внутри структуры в Octave. Это выглядит как

class =
    {
        grade = Graduate
        studentname = John
        university =  St. Jones
        student=
             {
                 name=John
                 age=18
                 address=Houston
             }

   } 

Чтобы реализовать эту структуру в структуре, я записываю

>> class.grade='graduate';
>> class.studentname='John';
>> class.university='St.Jones';

>> student.name='John';
>> student.age=18;
>> student.address='Houston';

>>student.class=struct %To create structure within a structure

Я получил такой вывод:

student =

scalar structure containing the fields:

name = John
age =  18
address = Houstan
class =

  scalar structure containing the fields:

Я не могу понять, почемуструктура класса здесь пуста? То же самое верно, если я попытаюсь запустить этот код следующим образом

>> class.student=struct

Вывод будет

class =

  scalar structure containing the fields:

grade = graduate
studentname = John
university = St.Jones
student =

  scalar structure containing the fields:

Пожалуйста, помогите мне решить мою проблему.

1 Ответ

1 голос
/ 02 октября 2019

Итак, с моей точки зрения, есть две возможности.

Либо настройте свою (под) структуру student, а затем установите class.student = student. Таким образом, поле student в class неявно становится (под) структурой. Код будет выглядеть так:

class.grade = 'graduate';
class.studentname = 'John';
class.university = 'St.Jones';

student.name = 'John';
student.age = 18;
student.address = 'Houston';

class.student = student

        class =

          scalar structure containing the fields:

            grade = graduate
            studentname = John
            university = St.Jones
            student =

              scalar structure containing the fields:

                name = John
                age =  18
                address = Houston

Или вы можете просто использовать вложенные структуры в начале, например так:

class.grade = 'graduate';
class.studentname = 'John';
class.university = 'St.Jones';

class.student.name = 'John';
class.student.age = 18;
class.student.address = 'Houston';

class

        class =

          scalar structure containing the fields:

            grade = graduate
            studentname = John
            university = St.Jones
            student =

              scalar structure containing the fields:

                name = John
                age =  18
                address = Houston

Опять же, поле student в classимплицитно создал (под) структуру.

Надеюсь, это поможет!

...