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

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 日
Thank you Adam. I tried your suggestion but does not work as aspected.
In particular, it work correctly if I extract ALL the element across the fields:
[Database(:).DisplAtReferenceTemperature]
But does not work if I ask only the n-th element (e.g. the second), as you suggested:
data = [Database(:).DisplAtReferenceTemperature(2)]
Indeeed I get the following error message:
>>> Expected one output from a curly brace or dot indexing expression, but there were 5 results.
Adam Danz
Adam Danz 2019 年 12 月 13 日
oops, just noticed that. Sorry. I updated my answer.
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 件)

カテゴリ

ヘルプ センター および File Exchange で Structures についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 12 月 13 日

編集済み:

2019 年 12 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by