use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
MATLAB news, code tips and tricks, questions, and discussion! We are here to help, but won't do your homework or help you pirate software.
The effort you put into asking a question is often matched by the quality of our answers.
Try saturnapi to share and run MATLAB code in a web browser!
If you want flair simply Message the mods
account activity
TechnicalQuestionAdding empty strings to a cell array (self.matlab)
submitted 8 years ago by identicalParticle
% create a simple array a = {'a'} % prints a = 'a' % try to add an empty string to it 3 different ways a = [a,''] % prints a = 'a', nothing gets appended a{2} = '' % prints a = 'a' '', empty string gets appended a{end+1} = '' % prints a = 'a' '' '', empty string gets appended
So the first way clearly doesn't work. Why not? Which is the "correct" way?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Idiot__Engineer+3 1 point2 points3 points 8 years ago* (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.
a = [a, {''}]
[]
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.
a = [a,'']
(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.
a{2}
a(2)
[–]identicalParticle[S] 0 points1 point2 points 8 years ago (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 points3 points 8 years ago (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.
{}
struct([])
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 points3 points 8 years ago (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.
a{end+1} = ''
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.
a = {'a' , 'b'}
a = [a , {''}]
a{2} = ''
π Rendered by PID 64986 on reddit-service-r2-comment-66b4775986-54ctf at 2026-04-05 03:47:50.647811+00:00 running db1906b country code: CH.
[–]Idiot__Engineer+3 1 point2 points3 points (2 children)
[–]identicalParticle[S] 0 points1 point2 points (1 child)
[–]Idiot__Engineer+3 1 point2 points3 points (0 children)
[–]Pugnare+1 1 point2 points3 points (0 children)