How to extract component of vector from a struct with fields across each field

조회 수: 55 (최근 30일)
It is more clear if I show you my specific case (see image below).
Let's assume that I created my struct variable called "Database" with many fields (e.g. measurement number).
Is there a simple method to get a 1D vector with the second element of Database.DisplAtReferenceTemperature, across all the fields?
I wonder to have a result equal to
[4712.9 5241.9 5681.2 6053.6 9305.8]
I tried the command
Database(:).DisplAtReferenceTemperature(2)
but does not work.
The "brute force" solution is to crete a for loop that scan all the fields, but in my opinion is not very efficient.

채택된 답변

Adam Danz
Adam Danz 2019년 12월 13일
편집: Adam Danz 2019년 12월 13일
To extract all element from the DisplAtReferenceTemperature fields, add square brackets (or curley brackets to store values in a cell array).
data = [Database(:).DisplAtReferenceTemperature];
To extract the 2nd element of DisplAtReferenceTemperature from each structure in the array Database,
data = arrayfun(@(i)Database(i).DisplAtReferenceTemperature(2),1:numel(Database))
  댓글 수: 4
Alberto Mora
Alberto Mora 2019년 12월 13일
편집: Alberto Mora 2019년 12월 13일
Wow, thank you. Now is perfect.
Take care that in your first answer that there is still a "(2)" that have to be deleted, regarding the extraction of all the elements. Update you answer such that also the other user can find easily the solution.
Thanks a lot!
Adam Danz
Adam Danz 2019년 12월 13일
편집: Adam Danz 2019년 12월 13일
Thank you for catching that! Fixed.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by