1
votes
>> v1
    [0.6324]    [0.0975]    [0.2785]

>> c1
    [0.8147]    [0.9058]    [0.1270]    [0.9134]

>> c1{1:3} = v1{1:3}

I get the following error message:

The right hand side of this assignment has too few values to satisfy the left hand side.

Here c1 and v1 are both simple cell arrays, ie, both have simple numerical values. Then, why shouldn’t this work?

1

1 Answers

3
votes

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]