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

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 comentarios

Please attach the table in a .mat file.
It would advisable for you to avoid refering to variables as "tables" unless they really are Matlab table variables.

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentarios

Thank you for your answer, however, this only selected the row within each trial that contains the massage 1. Could it be possible to select the entire trial that contains 1 in the massage. For example, in the table, there are 300 trials and trial 1 and 4 have 1 in the massage, and keep all the data in trial 1 and 4, not only the row with the massage 1.
Something like this?
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};
disp(selected);
Columns 1 through 11 {1×133 struct} {1×90 struct} {1×66 struct} {1×78 struct} {1×72 struct} {1×74 struct} {1×83 struct} {1×78 struct} {1×64 struct} {1×75 struct} {1×66 struct} Columns 12 through 22 {1×74 struct} {1×72 struct} {1×72 struct} {1×83 struct} {1×84 struct} {1×88 struct} {1×78 struct} {1×82 struct} {1×85 struct} {1×84 struct} {1×82 struct} Columns 23 through 33 {1×88 struct} {1×90 struct} {1×80 struct} {1×56 struct} {1×76 struct} {1×72 struct} {1×75 struct} {1×78 struct} {1×105 struct} {1×80 struct} {1×74 struct} Columns 34 through 44 {1×78 struct} {1×66 struct} {1×80 struct} {1×70 struct} {1×71 struct} {1×62 struct} {1×80 struct} {1×68 struct} {1×78 struct} {1×70 struct} {1×73 struct} Columns 45 through 55 {1×71 struct} {1×68 struct} {1×82 struct} {1×74 struct} {1×74 struct} {1×82 struct} {1×67 struct} {1×67 struct} {1×72 struct} {1×61 struct} {1×78 struct} Columns 56 through 66 {1×79 struct} {1×90 struct} {1×56 struct} {1×85 struct} {1×72 struct} {1×67 struct} {1×71 struct} {1×59 struct} {1×57 struct} {1×80 struct} {1×69 struct} Columns 67 through 77 {1×80 struct} {1×74 struct} {1×72 struct} {1×58 struct} {1×67 struct} {1×73 struct} {1×58 struct} {1×72 struct} {1×60 struct} {1×77 struct} {1×72 struct} Columns 78 through 88 {1×67 struct} {1×77 struct} {1×62 struct} {1×79 struct} {1×76 struct} {1×70 struct} {1×71 struct} {1×75 struct} {1×76 struct} {1×69 struct} {1×79 struct} Columns 89 through 99 {1×82 struct} {1×76 struct} {1×59 struct} {1×72 struct} {1×78 struct} {1×72 struct} {1×74 struct} {1×62 struct} {1×73 struct} {1×80 struct} {1×72 struct} Columns 100 through 110 {1×76 struct} {1×74 struct} {1×75 struct} {1×78 struct} {1×82 struct} {1×77 struct} {1×86 struct} {1×73 struct} {1×80 struct} {1×82 struct} {1×79 struct} Columns 111 through 121 {1×75 struct} {1×60 struct} {1×66 struct} {1×79 struct} {1×68 struct} {1×86 struct} {1×82 struct} {1×65 struct} {1×80 struct} {1×77 struct} {1×73 struct} Columns 122 through 132 {1×72 struct} {1×67 struct} {1×68 struct} {1×76 struct} {1×80 struct} {1×70 struct} {1×71 struct} {1×80 struct} {1×65 struct} {1×84 struct} {1×83 struct} Columns 133 through 143 {1×70 struct} {1×66 struct} {1×81 struct} {1×79 struct} {1×64 struct} {1×72 struct} {1×68 struct} {1×73 struct} {1×80 struct} {1×88 struct} {1×78 struct} Columns 144 through 150 {1×79 struct} {1×76 struct} {1×68 struct} {1×75 struct} {1×68 struct} {1×68 struct} {1×74 struct}
Yes! Thank you very much!
Voss
Voss el 7 de Feb. de 2023
Editada: Voss el 7 de Feb. de 2023
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
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!

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 7 de Feb. de 2023
Editada: Matt J el 7 de Feb. de 2023
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

Categorías

Productos

Versión

R2022b

Etiquetas

Preguntada:

JCH
el 7 de Feb. de 2023

Comentada:

JCH
el 8 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by