13-2 ?用?改變?構陣??資?

bc}CAڭ̥iHϥΡuyIv].^ӦsY@ƤY@ӯSwȡAҦp student(2).name NOĤGǥͪmWAstudent(3).scores(2) NOĤTǥͪĤGpҦZA̦CЬݤUCdҡG

Example 1: 13-c}C/accessStructData01.mclear student % M 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]); student(2).name='Alex'; % ܲĤGǥͪmW disp(student(2)); % ܲĤGǥͪ student(3).scores(2)=100; % ܲĤTǥͪĤGpҦZ disp(student(3)); % ܲĤTǥͪ name: 'Alex' scores: [80 85 90 88] name: 'Betty' scores: [88 100 90 80]

pGW٬O@ܼơAڭ̦Pˤ]iHϥΥyIӨoȡAbUCdҤAڭ̨ϥΤ@ܼ fieldName өwqAæCL쪺ȡAdҦpUG

Example 2: 13-c}C/accessStructData02.mclear student % M 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]); fieldName='name'; for i=1:length(student) fprintf('%d: Value of %s is %s\n', i, fieldName, student(i).(fieldName)); end 1: Value of name is Banny 2: Value of name is Joey 3: Value of name is Betty

MATLAB ѤF cat OAHFuñȡvتAykG

A = cat(dim, structureField)

䤤Adim NñƫҧܪסCҦp

dҦpUG

Example 3: 13-c}C/structCat01.mclear student % M 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]); score1 = cat(1, student.scores) % 1 NWUñƥHܾC score2 = cat(2, student.scores) % 2 NkñƥHܪ檺 score1 = 85 80 92 78 80 85 90 88 88 82 90 80 score2 = 85 80 92 78 80 85 90 88 88 82 90 80

biuñơvɡAT{QñƪȦۦPơ]WUñơ^ΦCơ]kñơ^A_hN|]פMӲͿ~TCҦpAN student ܼƪ name iWUñơAN|oͿ~AиոլݡGcat(1, student.name)C

̷ӤWҡAڭ̴NiHܧ֦ap⥭ơApUG

Example 4: 13-c}C/structAverage01.mclear student % M 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]); scoreMat = cat(1, student.scores); % 1 NWUñƥHܾC average1=mean(scoreMat); fprintf('|pҪZG'); disp(average1); average2=mean(scoreMat'); fprintf('TǥͪpҥG'); disp(average2); |pҪZG 84.3333 82.3333 90.6667 82.0000 TǥͪpҥG 83.7500 85.7500 85.0000

Hint
umean(A')v umean(A, 2)'vAиլݬݡIv

ѩuñƹBv`QΨAҥH MATLAB SѤUCؤkӶiȪñơG

dҦpUG

Example 5: 13-c}C/structLeftRightCat01.mclear student % M 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]); scoreMat = cat(1, student.scores); % 1 NWUñƥHܾC fprintf('ZkñơG'); disp([student.scores]) fprintf('mWkñơG'); disp({student.name})ZkñơG 85 80 92 78 80 85 90 88 88 82 90 80 mWkñơG 'Banny' 'Joey' 'Betty'

Wz cat OΤABjABⵥO MATLAB ثOABt׫ܧ֡CYznDBLkϥΤWzOӹFAunNϥγt׸C for jFI|ҨӻAYnLXCǥͪWrAiJpUG

Example 6: 13-c}C/structPrint01.mclear student % M student ܼ student(1) = struct('name', 'ix', 'scores', [85, 80]); student(2) = struct('name', 'ѻT', 'scores', [80, 85]); student(3) = struct('name', '', 'scores', [88, 82]); for i = 1:length(student) % CLXCӾǥͪWr fprintf ('student %g: %s\n', i, student(i).name); end student 1: ix student 2: ѻT student 3:

~Ai getfield setfield ӨoΧܤ@쪺ơAOϥή榡pUG

fieldValues = getfield(structureArray, {arrayIndex}, field, {fieldIndex})
newStructure = setfield(structureArray, {arrayIndex}, field,{fieldIndex})

ҦpAYoĤGǥͪĤ@pҦZAUCؼgkiHG

YNĤGǥͪĤ@pҦZאּ 100 AUCؼgk]iHG

Y@]whȡAi deal OAҦpG

Example 7: 13-c}C/structDeal01.mmyStruct = struct('name', {'Tim', 'Annie'}, 'age', {10, 13}); [myStruct.name] = deal('Roger', 'Jack'); fprintf('myStruct(1).name = %s\n', myStruct(1).name); fprintf('myStruct(2).name = %s\n', myStruct(2).name);myStruct(1).name = Roger myStruct(2).name = Jack

εc}CҦҦ쪺ơAi struct2cell OAҦpG

Example 8: 13-c}C/getStructData01.mclear student % M 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]); values = struct2cell(student) values(:,:,1) = 'Banny' [1x4 double] values(:,:,2) = 'Joey' [1x4 double] values(:,:,3) = 'Betty' [1x4 double]

Ъ`NbWҤAǦ^ values O@Ӳ}CAbѪe@СC@ӨAYJ struct2cell Ocܼƺ׬ mnAB]t p AhǦ^}C׬ pmnC]bWҤAp = 2Am = 1A n = 3C^


MATLAB{]pGJg