參考文獻
https://ww2.mathworks.cn/help/matlab/structures.html?searchHighlight=%E7%BB%93%E6%9E%84%E4%BD%93&s_tid=doc_srchtitle
https://ww2.mathworks.cn/help/matlab/matlab_prog/create-a-structure-array.html
https://ww2.mathworks.cn/help/matlab/matlab_prog/access-data-in-a-structure-array.html
https://ww2.mathworks.cn/help/matlab/matlab_prog/access-data-in-nested-structures.html
https://ww2.mathworks.cn/help/matlab/matlab_prog/access-multiple-elements-of-a-nonscalar-struct-array.htmlhtml
.1,.2,.3...
稱爲一個結構體的屬性patient(3).name = 'New Name'; patient(3) ans = struct with fields: name: 'New Name' billing: [] test: []
結構體也分爲標量結構體和結構體數組,結構體數組能夠經過結構體數組的索引進行訪問,而標量結構體能夠經過結構體名稱進行訪問。git
若是特定的字段包含 元胞數組 ,使用 花括號{} 訪問數據
github
S(2) = load('mandrill.mat')
S 是一個 1×2 的數組。數組
S = 1×2 struct array with fields: X map caption
對於非標量結構體,訪問特定字段的語法爲 structName(indices).fieldName。 從新顯示 clown 圖像,並指定 clown 結構體的索引 (1):函數
image(S(1).X) colormap(S(1).map) 添加索引以選擇並從新顯示字段內容的左上角: upperLeft = S(1).X(1:50,1:80); image(upperLeft)
僅當引用結構體數組的 單個元素 時,才能爲字段的部份內容創建索引。 MATLAB® 不支持諸如 S(1:2).X(1:50,1:80) 的語句,後者嘗試爲結構體的多個元素的字段創建索引。學習
s.n.a = ones(3); s.n.b = eye(4); s.n.c = magic(5);
third_row_b = s.n.b(3,:) 變量 third_row_b 包含 eye(4) 的第三行。 third_row_b = 0 0 1 0
s(1).n(2).a = 2*ones(3); s(1).n(2).b = 2*eye(4); s(1).n(2).c = 2*magic(5); s(2).n(1).a = '1a'; s(2).n(2).a = '2a'; s(2).n(1).b = '1b'; s(2).n(2).b = '2b'; s(2).n(1).c = '1c'; s(2).n(2).c = '2c'; 結構體 s 如今包含下圖中所示的數據。
part_two_eye = s(1).n(2).b(1:2,1:2) 這將返回 2*eye(4) 的左上角 2×2 的部分: part_two_eye = 2 0 0 2
s(1).f = 1; s(2).f = 'two'; s(3).f = 3 * ones(3);
s(1:3).f
或者 s.f
matlab 以逗號分隔列表的形式返回元素中的數據scala
ans = 1 ans = two ans = 3 3 3 3 3 3 3 3 3
[v1, v2, v3] = s.f; c = {s.f};
nums(1).f = 1; nums(2).f = 2; nums(3).f = 3; allNums = [nums.f] 該代碼返回 allNums = 1 2 3
numElements = arrayfun(@(x) numel(x.f), s) 語法 @(x) 能夠建立匿名函數。此代碼對數組 s 的每一個元素調用 numel 函數,例如 numel(s(1).f),並返回 numElements = 1 3 9