13-2 ?–ç”¨?Šæ”¹è®Šç?æ§‹é™£?—ç?è³‡æ?

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]

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)

• ±ý±N¤p¦Ò¦¨ÁZ¤W¤U¡]««ª½¡^¨Ã±Æ¡A¥i¿é¤J cat(1, student.scores)¡A¦¹®É³Q§ïÅÜªººû«×¬O²Ä¤@ºû¡C
• ±ý±N¤p¦Ò¦¨ÁZ¥ª¥k¡]¤ô¥­¡^¨Ã±Æ¡A¥i¿é¤J cat(2, student.scores)¡A¦¹®É³Q§ïÅÜªººû«×¬O²Ä¤Gºû¡C
½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

¨Ì·Ó¤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

¥Ñ©ó¡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'

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

• score3 = getfield(student, {2}, 'scores', {1});
• score3 = student(2).scores(1);

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

• student = setfield(student, {2}, 'scores', {1}, 100);
• student(2).scores(1)=100;