Vectorizing Table in Structure without Loop

3 ビュー (過去 30 日間)
Gabor
Gabor 2021 年 4 月 26 日
コメント済み: Gabor 2021 年 4 月 27 日
Hi,
I created this short code to explain my question:
for i=1:10
dynamic_field_name=string(cell_w_strings(i, 1)); %%% for eg.: "T1", "T2"
Table=Structure.(dynamic_field_name); %%% for eg.: Structure.T1 is a table
Date_v=Table.Date_column;
or in other words
Date_v{i}=Structure.T1.Date_column;
...
Date_v{i}=Structure.T2.Date_column;
end
I would like to vectorize this with no loop, something like with structfun cellfun or however it is possible:
Date_v{1}=Structure.T1.Date_column;
Date_v{2}=Structure.T2.Date_column;
...
Date_v{:}=Structure.(dynamic_structure_name(:)).Date_columns;
or
Date_v{:}=Structure.Cell{:}.Date_columns;
I understand that this might not be possible with dynamic field names in structure
But since I can change the dynamic field to a cell array than that is not the main point I am trying to ask.
The main question is how I can iterate through the tables in all structure fields to get the Date columns out and use them as vectors in an array
I just completely want to get wred of looping and create nice and sound vectorized script.
Thank you so much!

採用された回答

Mohammad Sami
Mohammad Sami 2021 年 4 月 27 日
編集済み: Mohammad Sami 2021 年 4 月 27 日
Are the Tables T1, T2, Tn compatible with each other, meaning they have the same column definitions.
Are the only data in the structure is T1, T2.. Tn.
If that is the case you can try doing this.
temp = struct2cell(s);
tall = vertcat(temp{:}); %concatenate all the tables.
If not then we need to do some extra processing.
temp = struct2cell(s);
temp = temp(cellfun(@istable,temp));
% assuming all table have Date_column
dts = cellfun(@(x)x.Date_column,temp,'UniformOutput',false);
dtsall = vertcat(dts{:});
  1 件のコメント
Gabor
Gabor 2021 年 4 月 27 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by