Assign a value to multiple cells in matlab

Flavian Hautbois picture Flavian Hautbois · Jul 12, 2012 · Viewed 32.6k times · Source

I have a 1D logical vector, a cell array, and a string value I want to assign.

I tried "cell{logical} = string" but I get the following error:

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

Do you have the solution?

Answer

user2000747 picture user2000747 · Jan 22, 2013

You don't actually need to use deal.

a = cell(10,1); % cell array
b = rand(1,10)>0.5; % vector with logicals
myString = 'hello'; % string

a(b) = {myString};

Looking at the last line: on the left hand side we are selecting a subset of cells from a and saying that they should all equal the cell on the right hand side, which is a cell containing a string.