How to work with Struct With Struct construction

5 views (last 30 days)
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

Jan
Jan on 16 Dec 2021
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
Ivan Ivan
Ivan Ivan on 16 Dec 2021
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
Ivan Ivan
Ivan Ivan on 16 Dec 2021
Guys, than you. I figured out this problem

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Dec 2021
names = structfun(@(S) S.Name, Data);
values = structfun(@(S) S.Value, Data, 'uniform', 0);
catval = cell2mat(values(:).')
  1 Comment
Ivan Ivan
Ivan Ivan on 16 Dec 2021
Didn't work. Error:
Error using structfun
Inputs to STRUCTFUN must be scalar structures.
Can you explain your code pls

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!