¦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
¦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
MATLAB ´£¨Ñ¤F cat «ü¥O¡A¥H¹F¨ì¡u¨Ã±ÆÄæ¦ìÈ¡vªº¥Øªº¡A¨ä»yªk¬°¡G
A = cat(dim, structureField)
¨ä¤¤¡Adim ¥Nªí¨Ã±Æ«á©Ò§ïÅܪººû«×¡C¨Ò¦p
- ±ý±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
¦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«Ü§Ö¦apºâ¥§¡¤À¼Æ¡A¦p¤U¡G
¥Ñ©ó¡u¨Ã±Æ¹Bºâ¡v±`³Q¥Î¨ì¡A©Ò¥H MATLAB ¤S´£¨Ñ¤U¦C¨âºØ¤èªk¨Ó¶i¦æÄæ¦ìȪº¨Ã±Æ¡G
- ¤è¬A©·¹Bºâ¡G¥i¥H¥ª¥k¦X¨Öµ²ºc°}¦C¤¤¬Û¦PÄæ¦ìªº¼Æȯx°}¡A²£¥Í¤@Ó·sªº¼Æȯx°}¡C
- ¤j¬A©·¹Bºâ¡G¥i¥H¥ª¥k¦X¨Öµ²ºc°}¦C¤¤¬Û¦PÄæ¦ìªº¦r¦ê¸ê®Æ¡A²£¥Í¤@Ó²§È¯x°}¡C
½d¨Ò¦p¤U¡G
¤Wzªº cat «ü¥O¤Î¤è¬A©·¡B¤j¬A©·¹Bºâµ¥³£¬O MATLAB ªº¤º«Ø«ü¥O¡A¨ä¹Bºâ³t«×«Ü§Ö¡C¦ýY±zn¨Dªº¹BºâµLªk¨Ï¥Î¤Wz«ü¥O¨Ó¹F¦¨¡A¥u¦n´N¨Ï¥Î³t«×¸ûºCªº for °j°é¤F¡IÁ|¨Ò¨Ó»¡¡AYn¦L¥X¨C¦ì¾Ç¥Íªº¦W¦r¡A¥i¿é¤J¦p¤U¡G
°£¦¹¤§¥~¡A¥ç¥i¥Î getfield ¤Î setfield ¨Ó¨ú±o¤Î§ïÅܤ@ÓÄæ¦ìªº¸ê®Æ¡A¨ä«ü¥O¨Ï¥Î®æ¦¡¦p¤U¡G
fieldValues = getfield(structureArray, {arrayIndex}, field, {fieldIndex})
newStructure = setfield(structureArray, {arrayIndex}, field,{fieldIndex})
¨Ò¦p¡AY±ý¨ú±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;
Y±ý¤@¦¸³]©w¦hÓÄæ¦ìÈ¡A¥i¥Î deal «ü¥O¡A¨Ò¦p¡G
±ý¨ú¥Îµ²ºc°}¦C¤¤©Ò¦³¤¸¯À¤º©Ò¦³Äæ¦ìªº¸ê®Æ¡A¥i¥Î struct2cell «ü¥O¡A¨Ò¦p¡G
½Ðª`·N¦b¤W¨Ò¤¤¡A¶Ç¦^ªº values ¬O¤@Ó²§½è°}¦C¡A¦b¥»®Ñªº«e¤@³¹¦³¬ÛÃöªº¤¶²Ð¡C¤@¯ë¦Ó¨¥¡AY¿é¤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