getfield to get values from an entire struct array?
조회 수: 35 (최근 30일)
이전 댓글 표시
I have an array of objects of type Peak. Given the name of a field (I will be using several in turn), I would like to get the value of that field for every object in the array, resulting in a vector of double. Is there any way to ask getfield to do that, without resorting to a for loop? Or is there some related function that would do the job for me?
채택된 답변
Bruno Luong
2024년 3월 29일
Create 1 x 3 struct array with field named 'afield'
s = struct('afield', {11 12 13})
s(1)
s(2)
s(3)
% Here is the command that might be usefule for you
A = cat(2, s.afield)
댓글 수: 3
Bruno Luong
2024년 3월 29일
편집: Bruno Luong
2024년 3월 29일
Take a look at dynamic fieldname
s = struct('afield', {11 12 13});
fieldname = "afield"
A = cat(2, s.(fieldname))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!