MATLAB: Dynamically update cell without clearing previous content

cell

Hi all,
I'd like to dynamically update cell content and size, without clearing the previous cell content. Check the following code:
clear; clc;
nr = 1;
for i = 1:5
a = cell(1, nr);
a{:, i} = {nr};
nr = nr + 1;
end
,
>> a
a =
[] [] [] [] {1x1 cell}
>> a{5}
ans =
[5]
Imagine I do not know how many iterations I will do, meaning cell size of 'a' needs to be dynamically updated with the number of iterations (which is why I use a = cell(1, nr) in the loop). However, after each iteration, content of 'a' is cleared, so I get only the last cell element of 'a'. But what I want is:
>> a
a =
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}
>> [a{:}]
ans =
[1] [2] [3] [4] [5]
I cannot move a = cell(1, nr) out of the for loop because in my real work I do not know how many iterations I will do. Any ideas? Thanks!

Best Answer

solution: 'a' can be dynamically updated without special treatment:
clear; clc;
nr = 1;
a = cell(1);
for i = 1:5
a{i} = {nr};
nr = nr + 1;
end