How to get an array of all field elements of a 1xN structure?

조회 수: 68 (최근 30일)
Sebastian
Sebastian 2019년 3월 14일
댓글: MD Titoo 2022년 11월 6일
Hi. I was not able to find an answer to my question, so I am asking it here. Is there a more direct way to get an array of all elements of a structure.field?
What I want is a direct one-liner which gives me the same as:
for ii = 1:length(Results)
Test(ii,1) = Results(ii).end_dates;
end
(Test is a 222x1 array)
something like:
Test = Results(:).end_dates
whereby this line somehow just gives me Results(1).end_dates, so just the first element of the field...
This is my structure: (a little bit small here, so also as an attachement)
  댓글 수: 2
Matt
Matt 2019년 3월 14일
Hi Sebastian,
You can do something like this with the commands:
Test = {Results.mid_time}
if you want the results in a cell array, or
Test = [Results.mid_time]
if you want the results in a vector.
Results.mid_time gives you a comma separated list of the elements, and the brackets {} or [] concatenate them together into an array.
I hope this helps.
Matt
Sebastian
Sebastian 2019년 3월 14일
편집: Sebastian 2020년 7월 2일
Yeah thank you very much!
Continually learning new stuff =)

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 3월 14일
편집: Adam Danz 2019년 3월 14일
If the values stored in .end_dates are scalars,
Test = [Results(:).end_dates]'; %Test will be column vector
If the values stored in .end_dates are row vectors of identical length,
Test = cell2mat({Results(:).end_dates}'); %Test will be matrix
If the values stored in .end_dates are column vectors of identical length,
Test = [Results(:).end_dates]; %Test will be a matrix
If the values stored in .end_dates are strings or matricies of uneqal size,
Test = {Results(:).end_dates}'; % Test will be a columnar cell array
  댓글 수: 3
Adam Danz
Adam Danz 2019년 3월 14일
Glad I could help!
MD Titoo
MD Titoo 2022년 11월 6일
thanks for the answer. Took me a while to find it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by