问题
I have simple code as below and try to insert the values into cell array.
a = cell(14,1);
for i = 1:14
a(i:1)=sin(i)
end
However error came out as:
Conversion to cell from double is not possible.
What is the problem for this code?
回答1:
Either expand the cell, or wrap the result of the sin function in a cell.
a = cell(14,1);
b = cell(14,1);
for ii = 1:14
a{ii} = sin(ii);
b(ii) = {sin(ii)};
end
isequal(a,b)
ans =
logical
1
回答2:
Your Syntax is wrong. a(i:1)
can not work inside a loop over i. Simply using a(i)
will give you the desired result.
来源:https://stackoverflow.com/questions/55184390/add-value-to-cell-in-a-loop