How to filter table rows according different conditions?

252 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2022 年 10 月 3 日
回答済み: dpb 2022 年 10 月 3 日
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,:)
NHATS_table_filtered = 1×11 table
Object Orbit ID H (mag) Estimated Diameter (m) OCC Min. delta-V [delta-V, dur.] (km/s), (d) Min. Duration [delta-V, dur.] (km/s), (d) Viable Trajectories Next Optical Opportunity (yyyy-mm [Vp]) Next Goldstone Radar Opportunity (yyyy-mm [SNR]) Var11 ______________ ________ _______ ______________________ ___ ________________________________________ _________________________________________ ___________________ _______________________________________ ________________________________________________ ____________ {'(2020 HO5)'} 5 28.5 {'3.5 - 16'} 4 {'3.874, 362'} {'11.997, 26'} 1.0614e+06 {'none'} {'none'} {'bK20H05O'}
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

採用された回答

Steven Lord
Steven Lord 2022 年 10 月 3 日
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"]), :)
ans = 2×2 table
LastName Age _________ ___ {'Smith'} 38 {'Jones'} 40
How about all patients whose names start with J?
T(startsWith(T.LastName, 'J'), :)
ans = 5×2 table
LastName Age ___________ ___ {'Johnson'} 43 {'Jones' } 40 {'Jackson'} 25 {'James' } 25 {'Jenkins'} 28
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'), :)
ans = 18×2 table
LastName Age ___________ ___ {'Brown' } 49 {'Miller' } 33 {'Taylor' } 31 {'Walker' } 28 {'Baker' } 44 {'Carter' } 38 {'Turner' } 37 {'Parker' } 30 {'Bell' } 45 {'Bailey' } 38 {'Cooper' } 28 {'Brooks' } 39 {'Bennett'} 35 {'Barnes' } 42 {'Butler' } 38 {'Foster' } 30

その他の回答 (1 件)

dpb
dpb 2022 年 10 月 3 日
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.

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by