MATLAB Answers

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

2,391 views (last 30 days)
fan feng
fan feng on 8 Jun 2015
Commented: Christopher Davis on 10 Jan 2021 at 9:27
>>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.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Jun 2015
A{end+1} = c;

  5 Comments

Show 2 older comments
Image Analyst
Image Analyst on 26 Nov 2019
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]
Caroline
Caroline on 17 Dec 2019
You can also use the cat command to "append to the beginning".
C = {"the" "end"};
C = cat(2,{"not"},C);
Christopher Davis
Christopher Davis on 10 Jan 2021 at 9:27
That's really helpful, not sure if it's proper terminology but I've heard this refered to as prepend

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!