12-4 ��L�������O

­Y­n¨Æ¥ý°t¸m¡]Pre-Allocate¡^ªÅªº²§½è°}¦C¡A¥i¥Î cell «ü¥O¡A¨Ò¦p¡G

Example 1: 12-²§½è°}¦C/cellAllocate01.mE = cell(4, 3) % ¨Æ¥ý°t¸m¤@­Ó 4¡Ñ3 ªÅªº²§½è°}¦C E = [] [] [] [] [] [] [] [] [] [] [] []

­Y­n´ú¸Õ¬Y¤@ÅܼƬO§_¬°²§½è°}¦C¡A¥i¥Î iscell «ü¥O¡A¨Ò¦p¡G

Example 2: 12-²§½è°}¦C/iscell01.mC = {[1,2,3]; 'This is a test.'}; iscell(C) ans = 1

¦b¤W¨Ò¤¤¡Aiscell «ü¥Oªº°õ¦æµ²ªG¬O¶Ç¦^­È 1¡A¥NªíÅÜ¼Æ A ¬O¤@­Ó²§½è°}¦C¡A­Y¶Ç¦^­È 0¡A«h¥Nªí¤£¬O²§½è°}¦C¡C

Hint
¦b MATLAB ¤¤¡A¥H¡§is¡¨¬°¦r­ºªº«ü¥O¤j³¡¤À¬OÀ˵ø«ü¥O¡A­YÀ˵øµ²ªG¬°¯u¡A«h¦^¶Ç¼Æ­È 1¡A§_«h´N¦^¶Ç¼Æ­È 0¡AÃþ¦üªº«ü¥O¦³ ischar¡Bisstr¡Bisnumeric¡Bisobject¡Bissparse¡Bisnan¡Bisfinite¡Bisinf¡Biscellstr¡Bisvarname¡Biskeyword µ¥¡C

num2cell «ü¥O¥i±N¤@¼Æ­È°}¦CÂà´«¦¨²§½è°}¦C¡A ¨ä¨Ï¥Î»yªk¬°¡G

C = num2cell(A, dim)

¨ä¤¤ dim ¬O¥Nªí ¡u³Q¤Á³Î¡vªººû«×¡A­Y¤£«ü©w¡A«h±N¨C­Ó¤¸¯Àµø¬°²§½è°}¦C¸Ì­±ªº¤@­Ó 1¡Ñ1 ¯x°}¡A¨Ò¦p¡G

Example 3: 12-²§½è°}¦C/num2cell01.mA = [1 2 3;4 5 6]; % «Ø¥ßªº¤@­Ó¼Æ­È°}¦C A C = num2cell(A) % ±N¼Æ­È°}¦C A Âন²§½è°}¦C C C = [1] [2] [3] [4] [5] [6]

­Y­n±N¼Æ­È°}¦C A ¤¤ªº¨C¤@ª½¦æ¡A Âন²§½è°}¦C D ªº¤¸¯À¡A¥i¿é¤J¦p¤U¡G

Example 4: 12-²§½è°}¦C/num2cell02.mA = [1 2 3;4 5 6]; % «Ø¥ßªº¤@­Ó¼Æ­È°}¦C A D = num2cell(A, 1) % 1 ¥Nªí¡u¾î¦C³Q¤Á³Î¡v D = [2x1 double] [2x1 double] [2x1 double]

±ý±N¼Æ­È°}¦C A ªº¨C¤@¾î¦C¡A Âন²§½è°}¦C E ªº¤¸¯À¡A¥i¿é¤J¦p¤U¡G

Example 5: 12-²§½è°}¦C/num2cell03.mA = [1 2 3;4 5 6]; % «Ø¥ßªº¤@­Ó¼Æ­È°}¦C A E = num2cell(A, 2) % 2 ¥Nªí¡uª½¦æ³Q¤Á³Î¡v E = [1x3 double] [1x3 double]

struct2cell «ü¥O¥i±N¤@µ²ºc°}¦Cªº©Ò¦³Äæ¦ì­ÈÂà´«¦¨²§½è°}¦C¡C¡]¦³Ãö©óµ²ºc°}¦Cªº»¡©ú¡A½Ð°Ñ¦Ò¥»®Ñ¤U¤@³¹¡uµ²ºc°}¦C¡v¡C¡^¨Ò¦p¡G

Example 6: 12-²§½è°}¦C/struct2cell01.mstudent.name = 'Tim'; student.age = 8; sCell = struct2cell(student) sCell = 'Tim' [8]

¥t¥~¦³¤@­Ó cell2struct «ü¥O¡A¥i¥H±N²§­È°}¦CÂà´«¬°µ²ºc°}¦C¡A¸Ô²Ó»¡©ú½Ð°Ñ¦Ò¥»®Ñªº¤U¤@³¹¡uµ²ºc°}¦C¡v¡C

­Y±ý±N¤@µ²ºc°}¦Cªº¬Y¤@Äæ¦ì­ÈÂà´«¦¨²§½è°}¦C¡A¥i¨Ï¥Î deal «ü¥O¡A¨Ò¦p¡G

Example 7: 12-²§½è°}¦C/deal01.mS = struct('name',{'Tim','Annie'},'age', {8,5}); [sCell{1:length(S)}] = deal(S.name) sCell = 'Tim' 'Annie'

deal «ü¥Oªº¿é¤J¤Î¿é¥X¤Þ­z»Ý¦³¬Û¦P­Ó¼Æ¡A¦]¦¹¦b¤W¨Ò¤¤¡AsCell{1:length(S)} ªº§@¥Î§Y¬O²£¥Í¥H³rÂI¤À¶}ªº¿é¥XÅܼƦC¡Cµ½¥Î deal «ü¥O¡A¥i¹F¦¨¨Æ¥b¥\­¿¤§®Ä¡A¨Ò¦p¡A±ý¦C¥X MATLAB ®Ú¥Ø¿ý¤U©Ò¦³¥Ø¿ý¡]¤£§tÀɮס^¡A¥i¿é¥X¦p¤U¡G

Example 8: 12-²§½è°}¦C/deal02.mdirInfo = dir(matlabroot); % ¦C¥X MATLAB ªº®Ú¥Ø¿ýªº¦UºØ¸ê°T n = length(dirInfo); % ÀɮפΥؿýªº­Ó¼Æ [fileAndDir{1:n}] = deal(dirInfo.name); % fileAndDir ¥]§t¦UÀɮפΥؿý¦WºÙ dirs = fileAndDir([dirInfo.isdir]) % dirs ¥]§t¦U¥Ø¿ý¦WºÙ dirs = Columns 1 through 6 '.' '..' 'appdata' 'bin' 'bugreport' 'etc' Columns 7 through 11 'examples' 'extern' 'help' 'java' 'lib' Columns 12 through 16 'licenses' 'mcr' 'notebook' 'polyspace' 'remote' Columns 17 through 21 'resources' 'rtw' 'runtime' 'simulink' 'stateflow' Columns 22 through 25 'sys' 'toolbox' 'ui' 'uninstall'


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