MATLAB Answers

0

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

Sebastian 님이 질문을 제출함. 14 Mar 2019
최근 활동 Adam Danz 님이 댓글을 추가함. 14 Mar 2019
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

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
Yeah thank you very much!
Continually learning new staff =)

로그인 to comment.

태그

답변 수: 1

Adam Danz 님의 답변 14 Mar 2019
Adam Danz 님이 편집함. 14 Mar 2019
 채택된 답변

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

  댓글 수: 2

Thank you!
So I missed the square brackets... ^^'
Glad I could help!

로그인 to comment.



Translated by