## 12-4 ?¶ä??¸é??‡ä»¤

­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]

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