title: Efficient allocation of cell array in matlab
body: <p>I have some which converts a cell array of strings into a cell array of characters.</p>
  
  <p>Note. For a number of reasons, both the input (C) and the output (C_itemised) must be cell arrays.</p>
  
  <p>The cell array of strings (C) is as follows:</p>
  
  <pre><code>&gt;&gt; C(1:10)
  
  ans = 
  
  't1416933446'
  ''
  't1416933446'
  ''
  't1416933446'
  ''
  't1416933446'
  ''
  't1416933446'
  ''
  </code></pre>
  
  <p>I have only shown a portion of the array here. In reality it is ~28,000 rows in length.</p>
  
  <p>I have some code which does this, although it is very inefficient. The cellstr function takes up 72% of the code's time, as it is currently called thousands of times. The code is as follows:</p>
  
  <pre><code>C_itemised=cell(length(C),500);
  for i=3:length(C)
      temp=char(C{i});
  
      for j=1:length(temp)
          C(i-2,j)=cellstr(temp(j));
      end
  end
  </code></pre>
  
  <p>I have a feeling that some minor modifications could take out the inner loop, thus cutting down the overall running time substantially. I have tried a number of ways to do this, but I think I keep getting confused about whether to use {} or (), and haven't been able to find anything online that can help me. Can anyone see a way to make the code more efficient?</p>
  
  <p>Please also note that this function is used in conjunction with other functions, and does work, although it is running slower than would be ideal. Therefore, I do not wish to change the format of <code>C_itemised</code>.</p>
  
  <p>EDIT:
  (A sample of) the output of my current function is:</p>
  
  <pre><code>C_itemised(1,1:12)
  
  ans = 
  
  Columns 1 through 12
  
  't'    '1'    '4'    '1'    '6'    '9'    '3'    '3'    '4'    '4'    '6'    []
  </code></pre>