フィルターのクリア

Extracting data from struct as array

6 ビュー (過去 30 日間)
Rashi Monga
Rashi Monga 2024 年 6 月 13 日
編集済み: Rashi Monga 2024 年 6 月 13 日
Hi, I have a following structure:
S(1).a = 1:10;
S(2).a = 11:20;
S(3).a = 21:30;
I want the output in the following format:
y = [1:10; 11:20; 21:30];
Using [S(1:3).a] concatenates it in a single direction. Is there a better way (other than for loop) to extract the data in the requisite format?
Thank you,
Rashi

採用された回答

Stephen23
Stephen23 2024 年 6 月 13 日
編集済み: Stephen23 2024 年 6 月 13 日
S(1).a = 1:10;
S(2).a = 11:20;
S(3).a = 21:30;
M = vertcat(S.a)
M = 3x10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
or
M = cat(1,S.a)
M = 3x10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

その他の回答 (1 件)

Ganesh
Ganesh 2024 年 6 月 13 日
You can use "vertcat()" for using the same. Refer to the following code:
% Your structure
S(1).a = 1:10;
S(2).a = 11:20;
S(3).a = 21:30;
% Extracting and concatenating
y = vertcat(S.a); % This works directly because of how MATLAB handles struct arrays
y
y = 3x10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
For more information on the function "vertcat()", refer to the following documentation:
  2 件のコメント
Rashi Monga
Rashi Monga 2024 年 6 月 13 日
編集済み: Rashi Monga 2024 年 6 月 13 日
Hi everyone,
Thank you. It was very helpful.
Ganesh
Ganesh 2024 年 6 月 13 日
% After using vertcat
y = y(:,1:5);

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by