How to work with Struct With Struct construction

5 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by