How to select multiple contain conditions in a table?

25 vues (au cours des 30 derniers jours)
Joy Shen
Joy Shen le 13 Juin 2023
Déplacé(e) : Stephen23 le 14 Juin 2023
Hi, I'm basically trying to filter my results to remove irrelevant rows based on two string conditions. I've attached the xslx file. I need to know the specific cause when the operating mode is 'critical' and the LOOP category is 'WR'. Basically isolating all the rows in the table where my two conditions are met.
Data = readtable('LOOP Events.xlsx','Range','D2:K370'); % Load excel from specified sheet
%LOOPS = cat(2,Data(:,1),Data(:,2),Data(:,3));
LOOPS = cat(2,table2array(Data(:,1)),table2array(Data(:,7)));%,table2array(Data(:,8)));
LOOPS = LOOPS(~all(cellfun(@isempty,LOOPS),2),:);
substr = {'Critical','SEE','EEE'};
selectedcol = contains(LOOPS,substr);
Cause = Data(:,3);
Select_Cause= Cause(selectedcol,:)

Réponse acceptée

Stephen23
Stephen23 le 13 Juin 2023
Modifié(e) : Stephen23 le 13 Juin 2023
The MATLAB approach:
T = readtable('LOOP Events.xlsx','Range','D:K');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T = rmmissing(T) % remove empty rows
T = 186×8 table
OperatingMode LOOPCategory LOOPClass SwitchyardRestorationTime PotentialBusRecoveryTime ActualBusRestorationTime Cause SpecificCause _____________ ____________ ______________ _________________________ ________________________ ________________________ _________ _______________ {'Critical'} {'SC'} {'LOOP-IE-I' } 15 28 28 {'HES' } {'Maintenance'} {'Critical'} {'SC'} {'LOOP-IE-I' } 0 4 4 {'Equip'} {'Breaker' } {'Shutdown'} {'SC'} {'LOOP-SD' } 15 28 28 {'HES' } {'Maintenance'} {'Shutdown'} {'SC'} {'LOOP-SD' } 77 82 82 {'Equip'} {'Other' } {'Shutdown'} {'SC'} {'LOOP-SD' } 62 63 63 {'Equip'} {'Transformer'} {'Critical'} {'SC'} {'LOOP-IE-I' } 95 118 213 {'Equip'} {'Breaker' } {'Shutdown'} {'WR'} {'LOOP-SD' } 528 533 533 {'SEE' } {'High Winds' } {'Critical'} {'SC'} {'LOOP-IE-I' } 1 2 3097 {'Equip'} {'Relay' } {'Critical'} {'WR'} {'LOOP-IE-I' } 1 16 7414 {'EEE' } {'Tornado' } {'Critical'} {'WR'} {'LOOP-IE-I' } 1 16 7414 {'EEE' } {'Tornado' } {'Shutdown'} {'SC'} {'LOOP-SD' } 39 44 44 {'Equip'} {'Transformer'} {'Critical'} {'WR'} {'LOOP-IE-I' } 1 16 7414 {'EEE' } {'Tornado' } {'Critical'} {'SC'} {'LOOP-IE-I' } 86 91 101 {'Equip'} {'Relay' } {'Critical'} {'PC'} {'LOOP-IE-NC'} 60 62 781 {'HE' } {'Switching' } {'Shutdown'} {'WR'} {'LOOP-SD' } 1120 1125 1508 {'SEE' } {'Salt Spray' } {'Shutdown'} {'SC'} {'LOOP-SD' } 15 30 136 {'HES' } {'Testing' }
X = matches(T.OperatingMode,'Critical') & matches(T.Cause,{'SEE','HEE'});
Select_Cause = T.LOOPClass(X)
Select_Cause = 19×1 cell array
{'LOOP-IE-NC'} {'LOOP-IE-NC'} {'LOOP-IE-NC'} {'LOOP-IE-NC'} {'LOOP-IE-NC'} {'LOOP-IE-I' } {'LOOP-IE-NC'} {'LOOP-IE-NC'} {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' } {'LOOP-IE-I' }
  1 commentaire
Joy Shen
Joy Shen le 14 Juin 2023
Déplacé(e) : Stephen23 le 14 Juin 2023
How elegant, thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by