Structure Arrays in MATLAB J S Roger Jang

Structure Arrays in MATLAB J. -S. Roger Jang 張智星 jang@cs. nthu. edu. tw http: //mirlab. org/jang CS Dept. , Tsing Hua Univ. , Taiwan

MATLAB 程式設計入門篇:結構陣列 Creation of Structure Arrays n Characteristics n n A structure array may contain elements. Each element may contain different fields. Each field of an element contain a variable of various data types. Example n n A set of students can be represented by a structure array. Each element of the array has fields of name、id、scores, etc.

MATLAB 程式設計入門篇:結構陣列 Example of Creation n n 範例13 -1 : struct 01. m clear student. name = 'Banny'; student. id = 'mr 871912'; student. scores = [58, 75, 62]; student % Clear student variable % Add name field % Add id field % Add score field % Display result student = name: 'Banny' id: 'mr 871912' scores: '[58, 75, 62]‘ “student” is the first element of an structure array.

MATLAB 程式設計入門篇:結構陣列 Example of Creation n 範例13 -2 : struct 02. m clear student. name = 'Banny'; student. id = 'mr 871912'; student. scores = [58, 75, 62]; % Add the second element to the structure array student(2). name = 'Raymond'; student(2). id = 'mr 872510'; student(2). scores = [25, 36, 92]; student = 1 x 2 struct array with fields: Name Id scores % % Clear student variable Add name field Add id field Add scores field % Display the result

MATLAB 程式設計入門篇:結構陣列 Usage of struct n n We can use struct command to create a structure array, as follows: structure. Array = struct(field 1, value 1, field 2, value 2, …. ) where field 1, … are the names of fields, and value 1, value 2, … are the values of the fields. If value 1, value 2, … are cell arrays, MATLAB will put each element of the cell arrays into each element of the structure array.



MATLAB 程式設計入門篇:結構陣列之範例五 n n 結構陣列可以是巢狀式(Nested)的,也就是說,結構陣列的欄位 可是另一個結構陣列,我們可以藉此產生複雜的資料結構 範例13 -5 : struct 05. m student = struct('name', {'張庭碩', '張庭安'}, 'scores', {[50 60], [60 70]}); student(2). course(1). title = 'Web Programming'; student(2). course(1). credits = 2; student(2). course(2). title = 'Numerical Method'; student(2). course(2). credits = 3; student(2). course ans = 1 x 2 struct array with fields: title credits

MATLAB 程式設計入門篇:結構陣列 13 -2 取用及改變結構陣列的資料 範例13 -6 : build. Struct 01. m clear student % Clear student(1) = struct('name', 'Banny', 'scores', [85, 80, 92, 78]); student(2) = struct('name', 'Joey', 'scores', [80, 85, 90, 88]); student(3) = struct('name', 'Betty', 'scores', [88, 82, 90, 80]); n 上述的 student 結構陣列,可圖示如下: n Student結構陣列 Student(1) Student(2) Student(3) . name=‘banny’ . name=‘joey’ . name=‘batty’ . scores=[85, 80, 92, 78] . scores=[80, 85, 90, 88] . scores=[88, 82, 90, 80]



MATLAB 程式設計入門篇:結構陣列 Data Aggregate n We can use cat command to aggregate same-field data, with the following usage: A = cat(dim, structure. Field) n To concatenate scores up and down: >> cat(2, student. scores) ans = 85 80 92 78 80 85 90 88 88 82 90 80 n To concatenate scores up and down: >> cat(1, student. scores) ans = 85 80 92 78 80 85 90 88 88 82 90 80



MATLAB 程式設計入門篇:結構陣列之範例七 範例13 -7 : print. Struct 01. m clear student % 清除 student 變數 student(1) = struct('name', '張庭碩', 'scores', [85, 80]); student(2) = struct('name', '鍾書蓉', 'scores', [80, 85]); student(3) = struct('name', '黃念中', 'scores', [88, 82]); for i = 1: length(student) % 列印出每個學生的名字 fprintf ('student %g: %sn', i, student(i). name); end student 1: 張庭碩 student 2: 鍾書蓉 student 3: 黃念中

MATLAB 程式設計入門篇:結構陣列 取得及改變欄位資料 n n n 亦可用 getfield及setfield來取得及改變一個欄位的資料 其指令使用格式如下: field. Values = getfield (structure. Array, {array. Index}, field, {field. Index}) new. Structure = setfield (structure. Array, {array. Index}, field, {field. Index}) 輸入下列運算式即可取得第二位學生的第一次小考成績: >> score 3 = getfield(student, {2}, 'scores', {1}) 上述 MATLAB 敘述可簡化為 >> score 3 = student(2). scores(1); 若欲改變第二位學生的第三次小考成績,可輸入如下: >> student = setfield(student, {2}, 'scores', {1}, 75); 上述敘述亦可簡化為 >> student(2). scores(1)=75;

MATLAB 程式設計入門篇:結構陣列之範例八 範例13 -8: deal 01. m my. Struct = struct('name', {'Tim', 'Annie'}, 'age', {10, 13}); [my. Struct. name] = deal('Roger', 'Sue'); fprintf('my. Struct(1). name = %sn', my. Struct(1). name); fprintf('my. Struct(2). name = %sn', my. Struct(2). name); my. Struct(1). name = Roger my. Struct(2). name = Sue


MATLAB 程式設計入門篇:結構陣列之範例十 n 範例13 -10: add. Field 01. m clear student % 清除 student 變數 student = struct('name', 'Roland', 'scores', [80, 90]); student(2). age = 20; % 加入新欄位 student(1) % 顯示 student(1) student(2) % 顯示 student(2) ans = name: 'Roland' scores: [80 90] age: [] ans = name: [] scores: [] age: 20 n 由上述結果可看出,MATLAB 會將此新欄位加入其他元素,並設 定其預設值為 [](空矩陣)。

MATLAB 程式設計入門篇:結構陣列之範例十一 n 範例13 -11: rm. Field 01. m student = struct('name', 'Roland', 'scores', [80, 90]) student 2 = rmfield(student, 'scores') % 刪除 scores 欄位 student = name: 'Roland' scores: [80 90] student 2 = name: 'Roland'

MATLAB 程式設計入門篇:結構陣列 13 -4 其他相關指令 n n n 我們可用 isstruct 指令來測試某個變數是否為結構陣列,如下: 範例13 -12 : isstruct 01. m s = struct('name', {'Tim', 'Ann'}, 'scores', {[1 3 5 ], [2 4 6]}); isstruct(s) ans = 1 isfield 指令則可用於測試某結構陣列是否含一特定欄位,例如: 範例13 -13 : isstruct 02. m s = struct('name', {'Tim', 'Ann'}, 'scores', {[1 3 5 ], [2 4 6]}); fprintf('isfield(s, ''name'') = %dn', isfield(s, 'name')); fprintf('isfield(s, ''height'') = %dn', isfield(s, 'height')); isfield(s, 'name') = 1 isfield(s, 'height') = 0 在上例中,因 s 並不包含“height”的欄位,故回傳數值 0。


MATLAB 程式設計入門篇:結構陣列之範例十五 n n 如果以 values 的第一個維度來對應欄位名稱 fields,結果如下: 範例13 -15 : cell 2 struct 02. m fields = {'name', 'age'}; values = {'Tim', 9; 'Annie', 6}; s = cell 2 struct(values, fields, 1); s(1) % 印出第一筆資料 s(2) % 印出第二筆資料 ans = name: 'Tim' age: 'Annie' ans = name: 9 age: 6

- Slides: 24