How to work with Struct With Struct construction

Hello.
I converted json.data to string and converted string to struct using jsondecode. Got the next struct like:
data =
struct with fields:
A1: [1×1 struct]
A2: [1×1 struct]
A3: [1×1 struct]
A4: [1×1 struct]
A5: [1×1 struct]
A6: [1×1 struct]
How to work with this construction, I mean let's say i need data from A1: [1x1 struct] (this structure contains the name of the variables and its value). How can I get it?
And can I convert this construction to massive with A1 ... A2 - rows and data from [1x1 struct] - columns?

 Accepted Answer

It is not clear, how the inputs data exactly look and what you want as output. Prefer to post some code, which produces both.
Maybe you want:
C = struct2cell(data);
Result = cat(1, C{:});

5 Comments

data looks like
{ "A1": { "1_par": val", "2_par": "val", "3_par": "val" }, "A2": { "1_par": val", "2_par": "val", "3_par": "val" }}
and some parameters contain another paramets like:
"n_par": { "n1_par": "val", "n2_par": "val", "n3_par": "val", "n4_par": "val", "n5_par": [ "val", "val", "val" ] }
completely looks like:
{ "A1": { "1_par": val", "2_par": "val", "3_par": val", "4_par": { "41_par": "val", "42_par": "val", "43_par": "val", "44_par": "val", "45_par": [ "val", "val", "val" ] } }, "A2": { "1_par": val", "2_par": "val", "3_par": "val" "4_par": { "41_par": "val", "42_par": "val", "43_par": "val", "44_par": "val", "45_par": [ "val", "val", "val" ] } }}
code:
fileName = 'data.json'; % filename in JSON extension
fid = fopen(fileName); % Opening the file
raw = fread(fid,inf); % Reading the contents
str = char(raw'); % Transformation
fclose(fid); % Closing the file
data = jsondecode(str)% Using the jsondecode function to parse JSON from string
I got struct with struct, but I don't know how to work with this
In that case, the answer to your question is NO, you cannot convert it into having the A* variables be rows and the values be columns -- not unless you make it a cell array. You have entries of different sizes. You have only 3 variables for A2, but you have 5 variables for your second example and one of those variables is a vector instead of a scalar. If you want an array that is not a cell array, everything has to be consistent.
I cannot run the code, because the input is not complete. Therefore I cannot guess, how your data looks like. I do not understand what "get struct with struct" means also.
data, but I cant download json file
Code:
function parsing()
fileName = 'data.txt'; % filename in JSON extension
fid = fopen(fileName); % Opening the file
raw = fread(fid,inf); % Reading the contents
str = char(raw'); % Transformation
fclose(fid); % Closing the file
data = jsondecode(str)% Using the jsondecode function to parse JSON from string
end
Guys, than you. I figured out this problem

Sign in to comment.

More Answers (1)

names = structfun(@(S) S.Name, Data);
values = structfun(@(S) S.Value, Data, 'uniform', 0);
catval = cell2mat(values(:).')

1 Comment

Didn't work. Error:
Error using structfun
Inputs to STRUCTFUN must be scalar structures.
Can you explain your code pls

Sign in to comment.

Asked:

on 16 Dec 2021

Commented:

on 16 Dec 2021

Community Treasure Hunt

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

Start Hunting!