Matlab中結構體struct創建和使用

在項目上遇見了調用api接口,接口返回的信息爲struct,故探討一下matlab的struct結構體

1、struct結構體創建
創建結構體數組有兩種方式,分別爲直接創建和使用struct函數

1.1 直接創建
直接定義字段,像使用一般matlab變量一樣,不需要事先聲明,支持動態擴充。

Student.name = 'wangx';
Student.sex = 'Male';
Student.height = '170';
 

對於多個結構體

Student(2).name = 'zhangsan';
Student(2).sex = 'Male';
Student(2).height = 172;

1.2 使用struct函數
函數使用方法:
s = struct(field1,value1,…,fieldN,valueN)

field1 = 'f1';  value1 = zeros(1,10);
field2 = 'f2';  value2 = {'a', 'b'};
field3 = 'f3';  value3 = {pi, pi.^2};
field4 = 'f4';  value4 = {'fourth'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4)

2、struct結構體使用
直接通過變量+.+變量名即可應用
在這裏插入圖片描述
PS:有定製開發、答疑需求,可以QQ聯繫:1762016542

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章