- If you're using a string array, use square brackets: ["Wall Jump", ..., "Move in contact"];
- When comparing strings or char-arrays, use strcmp() or strcmpi():. strcmpi(dat.NAME, Behaviours(k))
Error using *equal* when indexing information for creating a structure
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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)),:);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!