필터 지우기
필터 지우기

Error using *equal* when indexing information for creating a structure

조회 수: 1 (최근 30일)
Amy Hassett
Amy Hassett 2020년 3월 23일
편집: Adam Danz 2020년 3월 24일
Hi all,
I am trying to create a structure, by indexing from a cell array (which I define as follows):
Behaviours = {"Wall Jump"; "Move Isolated"; "Rear Isolated"; "SAP";
"Stop Isolated"; "Huddling"; "Contact"; "Move in contact"}
Here is the code for creating the structure:
for k =1:size(Behaviours)
AnimalData(k).behaviour = Behaviours(k);
AnimalData(k).All_Instances = dat(dat.NAME == Behaviours(k), :);
end
I wish to loop through each of these behaviours, and want to make a structure that contains all the information that I have in relation to each of these behaviours. However, when I try to pull the behavioural data from the "dat" structure that contains all my data I get the following error:
Error using == (line 25)
Invalid types for comparison.
Line 25 corresponds to the last line of my "for" loop. If I replace "Behaviours(k), :) with one of my behaviours, I do not get this error.
Can someone tell me what I am doing wrong?
  댓글 수: 3
Amy Hassett
Amy Hassett 2020년 3월 23일
To give you an idea, here is my dat Table.
Making the changes you recommend, here is the new code:
for k =1:size(Behaviours)
data(k).behaviour = Behaviours(k);
data(k).All_Instances = dat(strcmp(dat.NAME, Behaviours(k)));
end
When I run it, data.All_Instances is just a bunch of empty cells. Any advice?
Adam Danz
Adam Danz 2020년 3월 23일
I continued the discussion in the answers section.

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2020년 3월 23일
It doesn't look like you applied the recommendation correctly.
strcmp() returns a logical vector identifying which rows are a match. Try this.
data(k).All_Instances = dat(strcmp(dat.NAME, Behaviours(k)),:);
  댓글 수: 2
Amy Hassett
Amy Hassett 2020년 3월 23일
This gave an empty set, but it did make me think to try this:
for k =1:size(Behaviours)
data(k).behaviour = Behaviours(k);
data(k).All_Instances = dat(dat.NAME == data(k).behaviour, :);
end
Which worked!
Adam Danz
Adam Danz 2020년 3월 23일
편집: Adam Danz 2020년 3월 24일
If you attach a mat file containing the dat table, I could take a look at what's happening.
save('datData.mat', 'dat')
Are you sure the variables you're working with are string arrays rather than cell arrays of character vectors?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by