Why does matlab warn to preallocate a variable which is getting reset every loop?
<p>There is a cell variable in my program which Matlab warns to preallocate it. The simple form of code is sth like this:</p>

<pre><code>for i = 1:2
    a = [];
    a = [a,{'abc'}];
end
</code></pre>

<p>First I want to know why <code>a</code> should be preallocated, since it's getting reset in each loop. Second I don't know how to preallocate it. When I try to do so, Matlab gives me another warning, saying "The variable appears to be preallocated, but preallocation is not recommended here".</p>

<p>I use this code before the loop:</p>

<pre><code>a = cell(1,2);
</code></pre>

<p>To be more specific:</p>

<pre><code>for i = 1:2
    a = [];
    if condition1
        a = {'abc'};
    end
    if condition2
        a = [a,{'def'}];
    end
    b = [{'string'},a];
end
</code></pre>

<p>I want <code>b</code> to be a 1x1 cell array if the conditions are not true, so I need to reset <code>a</code> to an empty var in each loop.</p>

<p>Update:</p>

<p>I found a way, hinted by Lee's answer, but still doesn't know why. Using <code>a = [a(:),{'def'}];</code> instead of <code>a = [a,{'def'}];</code> solved the warning.</p>