How to append a new element to a cell object A?

>>A={'a';'b'};
I want to append a new element,say 'c', to A, how could I do?
I would appreciate if you could help me.

 Accepted Answer

A{end+1} = c;

6 Comments

Thank you so much.
Is there a way to "append to the beginning" of the cell array?
See this snippet. Hopefully it's self-explanatory.
% Create a new cell array.
ca = cell(1, 3);
for k = 1 : length(ca)
ca{k} = 1:k;
end
ca % Display in command window.
newStuff = rand(2); % Create some new data.
% Prepend it before cell #1, and show resulting array in the command window.
ca = [newStuff, ca]
You can also use the cat command to "append to the beginning".
C = {"the" "end"};
C = cat(2,{"not"},C);
That's really helpful, not sure if it's proper terminology but I've heard this refered to as prepend
Thank you, @Walter Roberson! @Christopher Davis, prepend is used for insertion of an element in the begining of a data structure. Append is the one used for end insertion as mentioned above.

Sign in to comment.

Products

Asked:

on 8 Jun 2015

Answered:

on 3 Dec 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!