user3594042
user3594042

Reputation: 5

Matlab bad cell reference operation when if statement

I have a <850x1> cell called x. Each of the individual structures has a 'Tag' name and 'Data' cell with <7168x1 double> data values.

(i.e. x{1,1}.Tag = 'Channel1', x{1,1}.Data= <7168x1 double>)

So, I want to go through the x cell, identify the structures with 'Channel1' Tag names and pull out that structure's data. Then, combine the data into a cell called Ch1. Here is my approach so far:

n=1:850
if x{n,1}.Tag == 'Channel1'
    Ch1{:,n} = x{n,1}.Data;
end

However, this gives an error: Bad cell reference operation.

Any ideas what may be going wrong?

Upvotes: 0

Views: 671

Answers (1)

ewz
ewz

Reputation: 423

There are 2 issues here. First, your if statement will compare each entry in the string x{n,1}.Tag to each entry in the string 'Channel1'. If the dimensions are not the same, you will get an error. To fix this, you could use the string compare function, strcmp. The other issue is that you are assigning n to all values between 1 and 850 at once. This is the issue that is producing the actual error you are seeing. Instead, you want to step through each of these values one at a time with a for loop. I would suggest trying the following code:

for n=1:850
   if strcmp(x{n,1}.Tag, 'Channel1')
       Ch1{:,n} = x{n,1}.Data;
   end
end 

Upvotes: 1

Related Questions