フィルターのクリア

How to select fields of a struct that contains certain string?

22 ビュー (過去 30 日間)
JCH
JCH 2023 年 2 月 7 日
コメント済み: JCH 2023 年 2 月 8 日
I have created a trial table of 1x300 struct with 1 field, and within every struct, there are five fields.
for every struct, there is one string in massage that is either 1 or 2, which indicates the type of the struct.
How can I select the structs that only have 1 in there? Thank you.
the table is attached.
  2 件のコメント
Matt J
Matt J 2023 年 2 月 7 日
Please attach the table in a .mat file.
Matt J
Matt J 2023 年 2 月 7 日
It would advisable for you to avoid refering to variables as "tables" unless they really are Matlab table variables.

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

採用された回答

Voss
Voss 2023 年 2 月 7 日
Maybe something along these lines:
load table
selected = [];
for ii = 1:numel(trial_table)
msg = {trial_table(ii).trial.message};
idx = strcmp(msg,'1');
selected = [selected trial_table(ii).trial(idx)];
end
disp(selected);
1×150 struct array with fields: message time code reltime pvel
  5 件のコメント
Voss
Voss 2023 年 2 月 7 日
編集済み: Voss 2023 年 2 月 7 日
You're welcome!
Note that I made selected a cell array because I assume you want to keep the trials separated (i.e., each cell of selected contains a struct array representing one trial), but if that's not the case, you can make selected itself a struct array:
load table
selected_trial_idx = [];
for ii = 1:numel(trial_table)
msg = {trial_table(ii).trial.message};
idx = strcmp(msg,'1');
if any(idx)
selected_trial_idx(end+1) = ii;
end
end
selected = [trial_table(selected_trial_idx).trial]; % use [] instead of {}
disp(selected);
1×11194 struct array with fields: message time code reltime pvel
JCH
JCH 2023 年 2 月 8 日
Thx, if I also want to mark their trials, saying these data are from trial 1 or 2, what should I do. Thank you again!

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

その他の回答 (1 件)

Matt J
Matt J 2023 年 2 月 7 日
編集済み: Matt J 2023 年 2 月 7 日
load table
for i=1:numel(trial_table)
s=trial_table(i).trial;
[~,loc]=ismember({'1','2'},{s.message});
result(i)=s(loc(loc~=0));
end
result
result = 1×300 struct array with fields:
message time code reltime pvel

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by