FunkyMore
FunkyMore

Reputation: 243

Add value to cell in a loop

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?

Upvotes: 0

Views: 79

Answers (2)

NewEyes
NewEyes

Reputation: 427

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.

Upvotes: 0

Paolo
Paolo

Reputation: 26230

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

Upvotes: 1

Related Questions