Index search in nested structure
Show older comments
Hi all,
I have a structure, which includes a nested structure.
It looks like this:
obj.characteristic1 = 'Test';
obj.characteristic2 = 'Test2';
obj.characteristic3 = 30;
obj.characteristic4 = struct('Name', [],...
'Age', [],...
'Height', [],...
'Gender', []);
The nested structure has 30 entries.
Target is to find a defined name, e.g. 'Tim'.
obj.characteristic4(1).name = 'Mike'
obj.characteristic4(2).name = 'Tim'
obj.characteristic4(3).name = 'David'
obj.characteristic4(4).name = 'Chris'
My solution on finding the index currently looks like this and is working.
fori=1:1:obj.characteristic3
if strcmp(obj.characteristic4(i).name,'Tim')
nameIndex = i;
end
end
The question is, whether there is a smarter way without a for-loop to find the index of the name field with the matching string.
Thank you for your help.
1 Comment
"...is a smarter way without a for-loop to find the index of the name field with the matching string"
As long as the parent structure is scalar, you can use a comma-separated list:
See Walter Roberson's answer for an example.
Accepted Answer
More Answers (0)
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!