13-2 ���ΤΧ��ܵ��c�}�C�����

¦bµ²ºc°}¦C¤¤¡A§Ú­Ì¥i¥H¨Ï¥Î¡u¥yÂI¡v¡]¡¨.¡¨¡^¨Ó¦s¨ú¬Y¤@µ§¸ê®Æ¤ºªº¬Y¤@­Ó¯S©wÄæ¦ì­È¡A¨Ò¦p student(2).name ´N¬O²Ä¤G¦ì¾Ç¥Íªº©m¦W¡Astudent(3).scores(2) ´N¬O²Ä¤T¦ì¾Ç¥Íªº²Ä¤G¦¸¤p¦Ò¦¨ÁZ¡A¨Ì¦¹Ãþ±À¡C½Ð¬Ý¤U¦C½d¨Ò¡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¦ì¾Ç¥Íªº©m¦W disp(student(2)); % Åã¥Ü²Ä¤G¦ì¾Ç¥Íªº¸ê®Æ student(3).scores(2)=100; % §ïÅܲĤT¦ì¾Ç¥Íªº²Ä¤G¦¸¤p¦Ò¦¨ÁZ disp(student(3)); % Åã¥Ü²Ä¤T¦ì¾Ç¥Íªº¸ê®Æ name: 'Alex' scores: [80 85 90 88] name: 'Betty' scores: [88 100 90 80]

¦pªGÄæ¦ì¦WºÙ¬O¤@­ÓÅܼơA§Ú­Ì¦P¼Ë¤]¥i¥H¨Ï¥Î¥yÂI¨Ó¨ú±o¬ÛÃöªºÄæ¦ì­È¡A¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î¤@­ÓÅÜ¼Æ fieldName ¨Ó©w¸qÄæ¦ì¡A¨Ã¦C¦L¦¹Äæ¦ìªº­È¡A½d¨Ò¦p¤U¡G

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 «ü¥O¡A¥H¹F¨ì¡u¨Ã±ÆÄæ¦ì­È¡vªº¥Øªº¡A¨ä»yªk¬°¡G

A = cat(dim, structureField)

¨ä¤¤¡Adim ¥Nªí¨Ã±Æ«á©Ò§ïÅܪººû«×¡C¨Ò¦p

½d¨Ò¦p¤U¡G

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 ¥Nªí¤W¤U¨Ã±Æ¥H§ïÅܾî¦Cªººû«× score2 = cat(2, student.scores) % 2 ¥Nªí¥ª¥k¨Ã±Æ¥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

¦b¶i¦æ¡u¨Ã±Æ¡v®É¡A¥²¶·½T»{³Q¨Ã±ÆªºÄæ¦ì­È¦³¬Û¦Pªº¦æ¼Æ¡]¤W¤U¨Ã±Æ¡^©Î¦C¼Æ¡]¥ª¥k¨Ã±Æ¡^¡A§_«h´N·|¦]¬°ºû«×¤£©M¦Ó²£¥Í¿ù»~°T®§¡C¨Ò¦p¡A±ý±N student Åܼƪº name Äæ¦ì¶i¦æ¤W¤U¨Ã±Æ¡A´N·|µo¥Í¿ù»~¡A½Ð¸Õ¸Õ¬Ý¡Gcat(1, student.name)¡C

¨Ì·Ó¤W¨Ò¡A§Ú­Ì´N¥i¥H«Ü§Ö¦a­pºâ¥­§¡¤À¼Æ¡A¦p¤U¡G

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 ¥Nªí¤W¤U¨Ã±Æ¥H§ïÅܾî¦Cªººû«× average1=mean(scoreMat); fprintf('¥|¦¸¤p¦Òªº¥þ¯Z¥­§¡¡G'); disp(average1); average2=mean(scoreMat'); fprintf('¤T¦ì¾Ç¥Íªº¤p¦Ò¥­§¡¡G'); disp(average2); ¥|¦¸¤p¦Òªº¥þ¯Z¥­§¡¡G 84.3333 82.3333 90.6667 82.0000 ¤T¦ì¾Ç¥Íªº¤p¦Ò¥­§¡¡G 83.7500 85.7500 85.0000

Hint
¡umean(A')¡v§¹¥þµ¥©ó ¡umean(A, 2)'¡v¡A½Ð¸Õ¬Ý¬Ý¡I¡v

¥Ñ©ó¡u¨Ã±Æ¹Bºâ¡v±`³Q¥Î¨ì¡A©Ò¥H MATLAB ¤S´£¨Ñ¤U¦C¨âºØ¤èªk¨Ó¶i¦æÄæ¦ì­Èªº¨Ã±Æ¡G

½d¨Ò¦p¤U¡G

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 ¥Nªí¤W¤U¨Ã±Æ¥H§ïÅܾî¦Cªººû«× fprintf('¦¨ÁZ¥ª¥k¨Ã±Æ¡G'); disp([student.scores]) fprintf('©m¦W¥ª¥k¨Ã±Æ¡G'); disp({student.name})¦¨ÁZ¥ª¥k¨Ã±Æ¡G 85 80 92 78 80 85 90 88 88 82 90 80 ©m¦W¥ª¥k¨Ã±Æ¡G 'Banny' 'Joey' 'Betty'

¤W­zªº cat «ü¥O¤Î¤è¬A©·¡B¤j¬A©·¹Bºâµ¥³£¬O MATLAB ªº¤º«Ø«ü¥O¡A¨ä¹Bºâ³t«×«Ü§Ö¡C¦ý­Y±z­n¨Dªº¹BºâµLªk¨Ï¥Î¤W­z«ü¥O¨Ó¹F¦¨¡A¥u¦n´N¨Ï¥Î³t«×¸ûºCªº for °j°é¤F¡IÁ|¨Ò¨Ó»¡¡A­Y­n¦L¥X¨C¦ì¾Ç¥Íªº¦W¦r¡A¥i¿é¤J¦p¤U¡G

Example 6: 13-µ²ºc°}¦C/structPrint01.mclear student % ²M°£ student ÅÜ¼Æ student(1) = struct('name', '±i®xºÓ', 'scores', [85, 80]); student(2) = struct('name', 'Áé®Ñ»T', 'scores', [80, 85]); student(3) = struct('name', '¶À©À¤¤', 'scores', [88, 82]); for i = 1:length(student) % ¦C¦L¥X¨C­Ó¾Ç¥Íªº¦W¦r fprintf ('student %g: %s\n', i, student(i).name); end student 1: ±i®xºÓ student 2: Áé®Ñ»T student 3: ¶À©À¤¤

°£¦¹¤§¥~¡A¥ç¥i¥Î getfield ¤Î setfield ¨Ó¨ú±o¤Î§ïÅܤ@­ÓÄæ¦ìªº¸ê®Æ¡A¨ä«ü¥O¨Ï¥Î®æ¦¡¦p¤U¡G

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

¨Ò¦p¡A­Y±ý¨ú±o²Ä¤G¦ì¾Ç¥Íªº²Ä¤@¦¸¤p¦Ò¦¨ÁZ¡A¤U¦C¨âºØ¼gªk³£¥i¥H¡G

­Y±ý±N²Ä¤G¦ì¾Ç¥Íªº²Ä¤@¦¸¤p¦Ò¦¨ÁZ§ï¬° 100 ¤À¡A¤U¦C¨âºØ¼gªk¤]³£¥i¥H¡G

­Y±ý¤@¦¸³]©w¦h­ÓÄæ¦ì­È¡A¥i¥Î deal «ü¥O¡A¨Ò¦p¡G

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¤¤©Ò¦³¤¸¯À¤º©Ò¦³Äæ¦ìªº¸ê®Æ¡A¥i¥Î struct2cell «ü¥O¡A¨Ò¦p¡G

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]

½Ðª`·N¦b¤W¨Ò¤¤¡A¶Ç¦^ªº values ¬O¤@­Ó²§½è°}¦C¡A¦b¥»®Ñªº«e¤@³¹¦³¬ÛÃöªº¤¶²Ð¡C¤@¯ë¦Ó¨¥¡A­Y¿é¤J struct2cell «ü¥Oªºµ²ºcÅܼƺû«×¬° m¡Ñn¡A¥B¥]§t p ­ÓÄæ¦ì¡A«h¶Ç¦^²§½è°}¦Cªººû«×¬° p¡Ñm¡Ñn¡C¡]¦b¤W¨Ò¤¤¡Ap = 2¡Am = 1¡A n = 3¡C¡^


MATLABµ{¦¡³]­p¡G¤Jªù½g