Accessing multiple fields in nested structures for string comparison
1 view (last 30 days)
Show older comments
Bob photonics
on 1 May 2018
Commented: Bob photonics
on 2 May 2018
I want to be able to access a specific field within my structure across multiple nested structures.
Let me explain what I want by an example:
I have a structure called
libinfo
This structure is a library where I'm going to save information and standard conventions for a specific toolbox. this structure has a substructure called:
raithinfo
raithinfo will contain multiple structure and be the general container for all the elements that my toolbox needs to draw/plot. (csf output file type)
Now what I want is to access a specific field in each of the sub-structures under raithinfo, I want to compare the string belonging to the field "Raithstrucname" to a specific string, so that I can find out what the index of the structure is within the raithinfo (so that I can extract specific information). My libinfo.raithinfo looks somhting like below if I type this into matlab
libinfo.raithinfo
ans =
[1x1 struct] [1x1 struct] [1x1 struct]
libinfo.raithinfo{:}
ans =
Raithstrucname: 'YsplitRef_ID_0003_L'
Raithstruc: [1x1 Raith_structure]
centerposition: {[0] [0]}
workingarea: {[-50] [-50] [50] [50]}
boundingbox: {2x4 cell}
positionlist: 0
ports: [1x1 struct]
ans =
Raithstrucname: 'CouplerRef_ID_WG0.5_P0.46_DC0.59_padw8_f10_g10_T250_DF1_L'
Raithstruc: [1x1 Raith_structure]
centerposition: {[0] [0]}
workingarea: {[-140] [-9] [140] [9]}
boundingbox: {2x1 cell}
positionlist: 0
ports: [1x1 struct]
ans =
Raithstrucname: 'CouplerRef_ID_0002_L'
Raithstruc: [1x1 Raith_structure]
centerposition: {[0] [0]}
workingarea: {[-140] [-9] [140] [9]}
boundingbox: {2x1 cell}
positionlist: 0
ports: [1x1 struct]
Now later on I will have to do the same for the positionlist, I will also need to check those values (check wether 0 or 1). Keep in mind that raithinfo might be quite a big list, a 1000 sub-structures wouldn't be weird, so I would like to use a method that's not really slow and if possible only 1 or 2 lines of code and not a for-loop, IF POSSIBLE.
I tried this
IndexC=find(ismember(libinfo.raithinfo{:}.Raithstrucname(:),couplername));
But that of course didn't work as it won't let me access them all at once
Currently I initialize my libinfo (library information) like this:
if isempty(libinfo)
libinfo(1).raithinfo{1}=struct('Raithstrucname',{nameL},'Raithstruc',{coupler_structL},...
'centerposition',{{0,0}},'workingarea',{WA},'boundingbox',{BD},'positionlist',{0},...
'ports',struct('in', struct('amount',{0},'positions',{},'angles',{}),...
'out',struct('amount',{1},'positions',{{cpl_length/2,0}},'angles',{{0}})));
elseif isempty(isempty(libinfo(1).raithinfo{1}))
libinfo(1).raithinfo{1}=struct('Raithstrucname',{nameL},'Raithstruc',{coupler_structL},...
'centerposition',{{0,0}},'workingarea',{WA},'boundingbox',{BD},'positionlist',{0},...
'ports',struct('in', struct('amount',{0},'positions',{},'angles',{}),...
'out',struct('amount',{1},'positions',{{cpl_length/2,0}},'angles',{{0}})));
else
libinfo(1).raithinfo{end+1}=struct('Raithstrucname',{nameL},'Raithstruc',{coupler_structL},...
'centerposition',{{0,0}},'workingarea',{WA},'boundingbox',{BD},'positionlist',{0},...
'ports',struct('in', struct('amount',{0},'positions',{},'angles',{0}),...
'out',struct('amount',{1},'positions',{{cpl_length/2,0}},'angles',{{0}})));
end
0 Comments
Accepted Answer
Jos (10584)
on 1 May 2018
Assuming libinfo is a scalar structure, and hence libinfo.Raithinfo is a cell array, you may benefit from using cellfun (or arrayfun)
tf1 = cellfun(@(C) ismember(C.Raithstrucname, couplername), libinfo.Raithinfo)
tf2 = arrayfun @(k) ismember(libinfo.Raithinfo{k}.Raithstrucname, couplername), 1:numel(libinfo.Raithinfo))
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!