how to extract all variables from a struct
조회 수: 206 (최근 30일)
이전 댓글 표시
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
답변 (4개)
Peter Jarosi
2019년 7월 30일
편집: Peter Jarosi
2019년 7월 30일
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
Rajesh
2019년 7월 30일
편집: Rajesh
2019년 7월 30일
댓글 수: 2
Stephen23
2019년 7월 30일
편집: Stephen23
2019년 7월 30일
"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:
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!