MATLAB One array database n elements 5 in


MATLAB 之 程應用 傳統的資料庫 • One array (database) • n elements (5 in here) • n field in each element (1 in here)

MATLAB 之 程應用 MATLAB give you much more flexibility


MATLAB 之 程應用 Arrangement of data in the structure array student.


MATLAB 之 程應用 範例1: struct 01. m n clear student % 清除 student 變數 student. name = ‘洪鵬翔’; % 加入 name 欄位 student. id = ‘mr 871912’; % 加入 id 欄位 student. scores = [58, 75, 62]; % 加入 scores 欄位 student % 秀出結果 student = name: '洪鵬翔' id: 'mr 871912' scores: '[58, 75, 62]‘ 此時 student 即代表一個結構陣列的第一個元素,或是第一筆資 料。





MATLAB 之 程應用 範例5: struct 05. m n 結構陣列可以是巢狀(Nested)的,也就是說,結構陣列的欄位可 是另一個結構陣列,我們可以藉此產生複雜的資料結構 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 之 程應用 範例6:build. Struct 01. m 取用及改變結構陣列的資料: clear student % 清除 student 變數 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 結構陣列,可圖示如下: Student結構陣列 1 x 3 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] 1 x 4




MATLAB 之 程應用 Multidimensional Arrays Consist of two-dimensional matrices “layered” to produce a third dimension. Each “layer” is called a page. cat(n, A, B, C, . . . ) Creates a new array by concatenating the arrays A, B, C, and so on along the dimension n.


MATLAB 之 程應用 計算平均: mean 指令 n mean 指令是對矩陣的每一直行進行平均值運算 >> cat(1, student. scores) ans = 85 80 92 78 80 85 90 88 88 82 90 80 計算每次考試(共四次)的平均分數,可輸入: >> mean(cat(1, student. scores)) ans = 84. 3333 82. 3333 90. 6667 82. 0000

MATLAB 之 程應用 計算平均: mean 指令 n 計算每位學生(共三位)的平均分數: >> average 2 = mean(cat(1, student. scores)') average 2 = 83. 7500 85. 0000


MATLAB 之 程應用 範例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 之 程應用 Common Structure functions

MATLAB 之 程應用 Structure functions Function Description Returns the field names = fieldnames(S) associated with the structure array S as names, a cell array of strings. F = getfield(S, ’field’) Returns the contents of the field ’field’ in the structure array S. Equivalent to F = S. field. isfield(S, ’field’) Returns 1 if ’field’ is the name of a field in the structure array S, and 0 otherwise.

MATLAB 之 程應用 Structure functions S = rmfield(S, ’field’) Removes the field ’field’ from the structure array S. S = setfield(S, ’field’, V) Sets the contents of the field ’field’ to the value V in the structure array S. S = struct(’f 1’, ’v 1’, ’f 2’, Creates a structure array with the fields ’f 1’, ’f 2’, . . ’v 2’, . . . ). having the values ’v 1’, ’v 2’, . .

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}) 可簡化為 >> score 3 = student(2). scores(1); 若欲改變第二位學生的第三次小考成績,可輸入如下: >> student = setfield(student, {2}, 'scores', {1}, 75); 可簡化為 >> student(2). scores(1)=75;

MATLAB 之 程應用 範例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 之 程應用 範例9: field. Names 01. m 取用及改變結構陣列的欄位 (fieldnames 指令) n n student = struct('name', 'Roland', 'scores', [80, 90]); all. Fields = fieldnames(student) all. Fields = 'name' 'scores' 傳回的結果是一個字串異質陣列(Cell Array of Strings),包 含了 student 的所有欄位。 欲增加一個新的欄位,直接將此欄加入於任一陣列元素即可

MATLAB 之 程應用 範例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 之 程應用 範例11:rm. Field 01. m student = struct('name', 'Roland', 'scores', [80, 90]) MATLAB 之 程應用 範例11:rm. Field 01. m student = struct('name', 'Roland', 'scores', [80, 90])](http://slidetodoc.com/presentation_image/4be7ce6e241ad1ce2640eb37de9646b5/image-30.jpg)
MATLAB 之 程應用 範例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' rm: remove

MATLAB 之 程應用 範例12: isstruct 01. m n 可用 isstruct 指令來測試某個變數是否為結構陣列: s = struct('name', {'Tim', 'Ann'}, 'scores', {[1 3 5 ], [2 4 6]}); isstruct(s) ans = 1

MATLAB 之 程應用 範例13: isstruct 02. m n 可用 isfield 指令測試某結構陣列是否含一特定欄位: 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 之 程應用 範例15:cell 2 struct 02. m n 如果以 values 的第一個維度來對應欄位名稱 fields,結果如下: fields = {'name', 'age'}; values = {'Tim', 9; 'Annie', 6}; 1 s = cell 2 struct(values, fields, ); s(1) % 顯示第一筆資料 s(2) % 顯示第二筆資料 ans = name: 'Tim' age: 'Annie' ans = name: 9 age: 6


MATLAB 之 程應用 練習:建立以下結構陣列 One array With 3 elements containing 6 fields

- Slides: 37