How to filter table rows according different conditions?
146 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi guys, I have a large table with 4480 rows and I need to filter them to mantain only rows matching a given condition.
clear all; close all; clc;
%Data import
file_name = 'NHATS_data.csv';
NHATS_table = readtable(file_name,"VariableNamingRule","preserve");
filt_cnd = strcmp(NHATS_table.Object,'(2020 HO5)');
% Other conditons on object names:
% ||... || (2021 GM1) || (2021 LF6) || (2020 CD3)
% || (2020 FA1) || (2020 HF4) || (2020 MU1) || (2020 WY)
% || (2021 AK5) || (2021 RZ3) || (2021 RG12) || (2022 BY39)
%Data filtering
NHATS_table_filtered = NHATS_table(filt_cnd,:)
I need to mantain all the rows that contains the objects whose names are the commentend ones. Up to now I managed to impose the condition on a single object name, can you help me to code in way that I can impost contemproary all the conditons?
Thanks in advance
0 Commenti
Risposta accettata
Steven Lord
il 3 Ott 2022
You can do this using ismember or (depending on your conditions) some of the string processing functions like matches, contains, startsWith, endsWith, etc.) Let's make a sample table.
load patients
T = table(LastName, Age);
Let's look for all patients whose names are Smith or Jones. For this I'll use ismember.
T(ismember(T.LastName, ["Smith", "Jones"]), :)
How about all patients whose names start with J?
T(startsWith(T.LastName, 'J'), :)
Starts with B or ends with r? For this we need to combine startsWith and endsWith.
T(startsWith(T.LastName, 'B') | endsWith(T.LastName, 'r'), :)
0 Commenti
Più risposte (1)
dpb
il 3 Ott 2022
NHATS_table = readtable(file_name,"VariableNamingRule","preserve");
NHATS_table.Object=categorical(NHATS_table.Object); % convert to categorical (several others probably should be as well...)
WANTLIST={'(2020 HO5)','(2021 GM1)','(2021 LF6)','(2020 CD3)','(2020 FA1)','(2020 HF4)','(2020 MU1)','(2020 WY)'};
NHATS_table_filtered = NHATS_table(ismember(NHATS_table.Object,WANTLIST),:);
NB: It may not be necessary to actually create the second table but use groupfilter with the given group.
0 Commenti
Vedere anche
Categorie
Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!