MATLAB Answers

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

1,795 views (last 30 days)
fan feng
fan feng on 8 Jun 2015
Commented: Caroline on 17 Dec 2019
>>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;

  4 Comments

Show 1 older comment
Henrique Menarin
Henrique Menarin on 26 Nov 2019
Is there a way to "append to the beginning" of the cell array?
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);

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products