How can I access a nested struct data by indexing of the fields?
Show older comments
Hi,
I'm trying to make loop to take some data from an url using webread. Each loop I collect a set of struct format data. Some field names are mainted (fixed) so I can use it in the loop. However, some names in struct data are specific, which difficults get them. I tried different ways of indexing but none worked.
Here an example of the loop
for i = 1%:numel(LotusID);
Prefixe = ('https://lotus.naturalproducts.net/api/search/simple?query=');
Sufixe = LTS0120864 %LotusID(i,1);
url_lotus = strcat(Prefixe,Sufixe);
html_lotus = webread(url_lotus);
end
I want access all information in this nested structure. However, I don't known the names of subfields to access and even create an indexing for them.
Here an example of the nested struct data.
html_lotus.naturalProducts.taxonomyReferenceObjects
Inside of this structure (html_lotus.naturalProducts.taxonomyReferenceObjects) there are 12 [1x1 struct]. So, I can't use an indexing because all them are [1x1 struct];
'x10_x_x_1021_NP980041X'
'x10_x_x_1021_NP900144C'
'x10_x_x_1248_CPB_x_x_57_x_x_302'
...
I want to get all information inside nested structured, but I dont know the names and I don't have indexing of them.
Specifically in this case (html_lotus.naturalProducts.taxonomyReferenceObjects.x10_x_x_1021_NP980041X) there are 3 anothers subsctructures for each one to them to then finally to get the data, properly.
How can I get this data. I tried to convert all nested into cells but didn't work
Thank you!
Alan
Accepted Answer
More Answers (1)
One quick and dirty solution that includes very unpopular eval function is given here:
Prefixe = ('https://lotus.naturalproducts.net/api/search/simple?query=');
Sufixe = 'LTS0120864'; %LotusID(i,1);
url_lotus = strcat(Prefixe,Sufixe);
html_lotus = webread(url_lotus);
fields = fieldnames(html_lotus);
var_struct = struct();
for i = 1:numel(fields)
eval_stri = ['html_lotus.' fields{i}];
var_aux = eval(eval_stri);
if isa(var_aux,'struct')
fprintf ('\n%s is a cell\n',fields{i});
var_struct = var_aux;
end
end
var_struct
Now, I'm sure some of the gurus will suggest more elgant way.
But in the code abive you can see how you can use webread and how to check if the variable is a structure, how to get fieldnames and use it to read members of nested structure. Now you can make this more deep in the same way.
Anyway the code above shows you how to get the names of the fields and how to create index of them, which was your initial question.
2 Comments
There's no need for eval here. Use dynamic field names.
s = struct('x', 1, 'y', 2, 'z', 3)
f = 'y';
two = s.(f) % 2
Alternately if you have multiple levels of indexing you can use getfield.
nested = struct('w', 4, 'q', s)
thefields = {'q', 'z'};
three = getfield(nested, thefields{:}) % nested.q.z
Askic V
on 9 Feb 2023
Thank you Steven. Very helpful.
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!