?? 2-40.asv
字號:
patient.name = 'John Doe';
patient.billing = 127.00;
patient.test = [79 75 73; 180 178 177.5; 220 210 205];
% 創建圖2-7的結構數組,這是一個單結構數組
patient
% 顯示結構數組
patient(2).name = 'Ann Lane';
patient(2).billing = 28.50;
patient(2).test = [68 70; 118 118];
% 向結構數組添加一個元素,變成1×2的結構數組
patient
% 顯示結構數組
patient(3).name = ['Ann Lane','John '];
% 結構數組第三個元素的name域值為多行字符串
patient(3).billing = [44 28.50];
% 結構數組第三個元素的billing域值為1×2的數組
patient(3).test = {'This is an example!',[1 2;3 4]};
% 結構數組第三個元素的test域值為一個元胞數組
s1 = struct('type', {'big','little'}, 'color', {'red'}, 'x', {3 4})
% 創建一個1×2的結構數組,這里{'big','little'}、{3 4}是元胞數組,花括號里面字符串和數值分別為結構數組兩元素的域值
s2 = struct('type', {{'big','little'}}, 'color', {'red'}, 'x', {{3 4}})
% 創建一個1×1的結構數組,這里{'big','little'}、{3 4}也是元胞數組,但它們是域值,比較和s1的差別
s3 = struct('type', {'big','little','middle'}, 'color', {'red'}, 'x', {3 4})
% 無法創建結構數組,這里其實想創建一個1×3的結構數組,但是{3 4}輸入量為2
s4 = struct('type', {'big','little','middle'}, 'x', [])
% 創建x域值為空的結構數組,結構數組大小為1×3
s4 =
1x3 struct array with fields:
type
x
s5 = struct( 'y3',{'1'},'x', {})
% 創建一個空結構數組
s5 =
0x0 struct array with fields:
y3
x
weather = repmat(struct('temp',72, 'rainfall', 0.0), 2, 3)
% 用repmat復制創建一個2×3的結構數組
weather =
2x3 struct array with fields:
temp
rainfall
另外struct函數不能直接創建嵌套的結構數組。嵌套的結構數組需要另外單獨創建。可以先用s4的方法,用“[]”賦值為空,然后創建數組后再另行創建。如下:
s4(2).x.test = [1 2;3 4];
% s4(2)的x域值也是一個結構
s4(2)
ans =
type: 'little'
x: [1x1 struct]
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -