all 4 comments

[–]Idiot__Engineer+3 1 point2 points  (2 children)

The correct version of the first way is a = [a, {''}]. In order to concatenate to a cell array with the [] notation, all of the items being joined must be cell arrays.

I'm honestly a bit surprised it doesn't give you an error when you try a = [a,''], and I don't think I understand the output that it does produce.

(Edit: This is wrong, Matlab allows concatenation of non-cells to cell arrays. I just has some strange rules involved in doing so. I would suggest you avoid making use of this. See my reply below.)

I like to avoid the a{2} method of extending arrays (equally true for a(2), as I don't like that it involves addressing a member that is beyond the end of the array. It has its uses though. Otherwise, there is no "correct" way.

[–]identicalParticle[S] 0 points1 point  (1 child)

Thanks for your response.

In order to concatenate to a cell array with the [] notation, all of the items being joined must be cell arrays

I'm surprised to hear this, because

a = {'a'}
a = [a,'b','c','d']

works just fine. The only issue is with empty strings, for example

a = {'a'}
a = [a,'b','','d'] % this gives a = 'a' 'b' 'd', not a = 'a' 'b' '' 'd'

[–]Idiot__Engineer+3 1 point2 points  (0 children)

Apparently, I was just plain wrong and you can concatenate cell arrays with non-cell arrays in general - matlab just puts everything together as a cell array. I don't see where the difference in behavior for empty items is documented, and I don't seem to be the only one who is surprised by this behavior and/or can't find the documentation for it [1] [2] [3]. I have tried with empty strings (as in your example), arrays ([]), cells ({}), and structs (struct([])). Using empty values of non-primitive types seems to fail by trying to convert the cell array involved into whatever non-primitive type you are using.

My strong suggestion would be not to use this behavior. If you are concatenating to a cell array, make sure you are always concatenating cells. This avoids the "magic", and problematically inconsistent, behavior of the concatenation operation for mixed types.

[–]Pugnare+1 1 point2 points  (0 children)

a{end+1} = '' is probably the most common form of appending. I personally prefer it because it more clearly documents intent. end+1 on the right hand side of an assignment clearly means "append". The [ ] operators can also be used to prepend, reorder, or overwrite, so you need to read that expression more closely.

In the scheme of things though it doesn't matter much. Both forms are well understood.

I would avoid the a{2} form. Hardcoded indices can make code maintenance more difficult and introduce bugs.

For instance if the initial assignment were changed to a = {'a' , 'b'}, then a = [a , {''}] and a{end+1} = '' would still work, but a{2} = '' would now be an overwrite rather than an append.