MATLAB: Haw to add new element of cell arrays

arraycellstring

I have a cell array of strings: {'abc','xyz','123'} and I would like to add a new element 'something' But I don't want to calculate the length of the array. Is it possible to join a new element to the end?

Best Answer

You can use the end operator:
A = {'abc','xyz','123'};
A{end+1} = 'something'
Note that while this is okay to do a few times within the code, do not do this inside a loop, as this continually expands the array and MATLAB must check the available memory and move the array as it enlarges. This is very inefficient and slow. Instead before a loop you should preallocate the cell array. This advice also applies to preallocating numeric arrays.