how to extract all variables from a struct
Ältere Kommentare anzeigen

Hi, I wanted to extract all the variables from the struct without having to manually type the headers. In short I want to dissolve the entire struct to differnt variables with their individual header names as the variable name.
Many thanks in Advance
Antworten (4)
Andrei Bobrov
am 30 Jul. 2019
T = struct2table(data_table);
1 Kommentar
Stephen23
am 30 Jul. 2019
+1 excellent idea.
Peter Jarosi
am 30 Jul. 2019
Bearbeitet: Peter Jarosi
am 30 Jul. 2019
I agree with Stephen but if you want a quick solution using an ugly trick:
v = fieldnames(data_table);
for x = 1 : length(v)
myVar = data_table.(v{x})
end
or a little bit less ugly:
for v = fieldnames(data_table)
myVar = data_table.(v{1})
end
3 Kommentare
@Peter Jarosi: sadly our good advice was ignored. Sadly someone even voted for the anti-pattern eval answer.
Peter Jarosi
am 30 Jul. 2019
Peter Jarosi
am 30 Jul. 2019
2 Kommentare
"i had to use eval"
I doubt that you "had to use eval". I have written thousands of lines of code and imported thousands of data files of many different formats, and yet I have never needed to use eval.
You just designed your code (or data) badly, such that you force yourself into writing slow, buggy, complex, obfuscated code that is hard to debug:
Jan
am 30 Jul. 2019
Using eval is indeed a very bad idea.
Rajesh
am 30 Jul. 2019
0 Stimmen
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!