Access the data from a Struct

I have a struct with fields
val = struct with fields:
abstract: [1×1 struct]
body_text: [72×1 struct]
when I extract the text data in body_text using "val.body_text.text" I got 72*1 struct. But when I try "A = val.abstract.text", I got only the first struct of the 72 structs. How can I put all these [72×1 struct] into A?
Thanks!

3 comentarios

dpb
dpb el 30 de Mzo. de 2020
"when I try "A = val.abstract.text"
But
val = struct with fields:
abstract: [1×1 struct]
body_text: [72×1 struct]
so val.abstract.text is either invalid reference or is the name of the 1x1 struct under val.abstract
The array of 72 stuct is contained in the val struct field .body_text
It isn't clear why you think there should be more than one element from the reference above...of course, we can't see what the content of these embedded struct is so we've no idea what are fieldnames thereof.
Seems like a very convoluted nested storage--my first recommendation would be to see if you can't somehow simplify this drastically.
Ameer Hamza
Ameer Hamza el 30 de Mzo. de 2020
''so val.abstract.text is either invalid reference or is the name of the 1x1 struct under val.abstract'''
Not necessarily. For example
>> s.text = '123';
s.xyz = 2;
val.abstract = s;
val.body_text = repmat(s, 1, 72);
>> val
val =
struct with fields:
abstract: [1×1 struct]
body_text: [1×72 struct]
>> val.abstract.text
ans =
'123'
Susan
Susan el 30 de Mzo. de 2020
Sorry for the confusion! What I meant was "But when I try "A = val.body_text.text", I got only the first struct of the 72 structs"

Iniciar sesión para comentar.

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 30 de Mzo. de 2020
Editada: Ameer Hamza el 30 de Mzo. de 2020
Try this, if the data in the field 'text' is scalar
A = [val.abstract.text];
If it is vector than try
A = {val.abstract.text};

5 comentarios

Susan
Susan el 30 de Mzo. de 2020
Thank you very much! Your answer solved my issue.
Ameer Hamza
Ameer Hamza el 30 de Mzo. de 2020
Glad to be of help.
Susan
Susan el 30 de Mzo. de 2020
I've got another question regarding the nested struct and your answer would be greatly appreciated.
I have a bunch of .json files, say 1000. To read each files I run the following code
fname = 'C:\Users\...\d90f3c62681e.json';
val = jsondecode(fileread(fname));
then I get
val =
struct with fields:
paper_id: 'd90f3c62681e'
metadata: [1×1 struct]
abstract: [1×1 struct]
body_text: [4×1 struct]
bib_entries: [1×1 struct]
ref_entries: [1×1 struct]
back_matter: []
val.abstract =
struct with fields:
text: '300 words) The data ...'
cite_spans: []
ref_spans: []
section: 'Abstract'
val.body_text =
4×1 struct array with fields:
text
cite_spans
ref_spans
section
which the name of each file is the paper_ID. I am only interested in the text data in val.abstract.text and val.body_text.text which their sizes may change from file to file. How can I get all the text data from each file and put them in a single .json files? I have attached 3 .json files from the data set I have. Any input would be greatly appreciated.
Ameer Hamza
Ameer Hamza el 31 de Mzo. de 2020
Try the following code. Place it one directly above the JSON files, or you can modify the first line according to the path of your JSON files. It will read all files, and gather their abstract.text and body_text.text fields into one struct. It then writes the final struct as a JSON file.
files = dir('JSON files/*.json');
s = struct('abstract', [], 'body_text', []);
for i=1:numel(files)
filename = fullfile(files(i).folder, files(i).name);
data = jsondecode(fileread(filename));
if ~isempty(data.abstract)
s.abstract = [s.abstract; cell2struct({data.abstract.text}, 'text', 1)];
end
if ~isempty(data.body_text)
s.body_text = [s.body_text; cell2struct({data.body_text.text}, 'text', 1)];
end
end
str = jsonencode(s);
f = fopen('filename.json', 'w');
fprintf(f, '%s', str);
fclose(f);
Susan
Susan el 31 de Mzo. de 2020
Thank you so much! I really appreciate your help.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 30 de Mzo. de 2020

Comentada:

el 31 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by