When using curly brackets {}
, you are extracting the values of the cells. Use normal brackets ()
to refer to a set of cells and therefore keep the cells without extracting the actual content. So the following line will assign the cells (not the values inside the cells) from the right hand side to the left hand side:
c1(1:3) = v1(1:3)
We can check the datatype of c1(1:3)
easily and see that it is in fact a cell-array:
>> A = c1(1:3)
A =
[1] [2] [3]
>> class(A)
ans =
cell
To see that curly brackets {}
extract the values itself, we can do the following and see that the datatype of B
is double:
>> B = c1{1}
B =
1
>> class(B)
ans =
double
As @Dan mentions in his comment, v1{1:3}
gives you a comma-separated list of three separate doubles. You can notice that by seeing three ans =
using the command line, because all the values will be returned individually:
>> v1{1:3}
ans =
1
ans =
2
ans =
3
Following that, you can do the assignment in a different way, which I do not recommend. The following provides three elements on the LHS by using the concatenation operator []
, the RHS provides three elements as well as we saw above.
>> [c1{1:3}] = v1{1:3}
c1 =
[1] [2] [3] [7]