Converting a vector of structs to just one struct (vectorizing)

So i have this struct and in it there are, for example, 402 1x1 structs with the same variables (not same values of course).
What i need is to all that structs to become just one (or none at all, just the variables). In other words, if i have 402 user variables in the shape
[1 2 3], i want to make a 402x3 variable.

 採用された回答

Jan
Jan 2022 年 7 月 18 日
X = cat(1, S(:).FieldName);
% Or shorter:
X = cat(1, S.FieldName);
% Example:
S(1).FieldName = [1,2,3];
S(2).FieldName = [4,5,6];
X = cat(1, S.FieldName)
X = 2×3
1 2 3 4 5 6

3 件のコメント

Emanuel
Emanuel 2022 年 7 月 18 日
Will this give me a 1x1 struct with 2 fields (1 of those fields being, e.g., the 402x3 previously said)?
I have 402 1x1 structs, with the fields "channel" and "loc" in each one of those structs. Considering "loc" as a 1x3 vector, i want to make this as a 402x3 matrix, so loc will be one field of two in a 1x1 struct with 2 fields.
Jan
Jan 2022 年 7 月 18 日
I cannot know, what "I have 402 1x1 structs" exactly means. Is this a struct array or are they 402 different variables (which would be a very bad idea)?
I've showed how to extract the data of a field stored in a struct array. How to apply it to your data would get clear, if you explain exactly, what kind of data you have. Prefer to post some code which creates example data.
Emanuel
Emanuel 2022 年 7 月 18 日
It is a struct array, yes.
After your example i've tried a more low-level approach of extracting the data and it worked as i expected. Thanks for the help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2019b

質問済み:

2022 年 7 月 18 日

コメント済み:

2022 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by