Delete rows of table through search

3 ビュー (過去 30 日間)
Karina
Karina 2018 年 4 月 25 日
回答済み: Jon 2018 年 4 月 25 日
I have a table 669602 x 5 table and I want to delete all the rows when one of the columns has a value of zero. My code is below:
if true
% code
files = what('March\MatFile'); %list all the mat-files in the folder
num = length(files.mat);
folder = dir('March\MatFile'); var = zeros([num,1]); ActualGenTable = table(var, var, var, var, var); ActualGenTable.Properties.VariableNames = {'n','GenID','GenCount', 'TotalGenMW', 'TotalGenMWh'}; tic for i = 1:num Gen = files.mat{i}; %generator mat file split1 = strsplit(Gen,'.'); GenFileName = split1{1}; split2 = strsplit(GenFileName, '_'); GenIDName = split2{2}; GenID = split2(2); GenNum = str2num(GenIDName);
S = load(['March\MatFile\' Gen]); %structure
T = S.T; %
row = height(T);
newtable = T;
Col = newtable.ActualGen;
SumNonzero = sum(Col(:,1) ~= 0);
toDelete = newtable.ActualGen == 0;
one = array2table(toDelete);
sumd = sum(one.toDelete1);
deletetotal = sum(sumd);
if deletetotal ~= 0
newtable(toDelete,:) = []; %row which returns error
end
count2 = height(newtable);
name = ['ActualGen.' GenFileName '.mat'];
save(name,'newtable');
toc
end
Tname = 'ActualGenerationIndex.mat';
save(Tname,'ActualGenTable');
end
In the first iteration the code runs fine with no errors because the first data set does not have any zeros. In the second iteration there are 4525 columns with zeros however it records an error for:
Error using ActualGenExtraction (line 34) Row index exceeds table dimensions.

回答 (1 件)

Jon
Jon 2018 年 4 月 25 日
I'm not sure from your description exactly what you are trying to do, but I assume it is one of the following two use cases. Use Case 1, you want to eliminate rows where some particular variable (column) in the table has a zero entry, Use Case 2, you want to eliminate rows where any variable has a zero entry. You can do this quite directly using logical indices as shown in the example code below. You would of course have to adapt the general approach demonstrated in the example script to your specific situation.
% There are zero entries in row 1 in the fourth column (tblA.d) and in row
% 3 in the third column (tblA.c)
A = [1 3 8 0;2 6 3 8;3 9 0 2;4 4 9 2; 5 8 2 7 ];
tblA = array2table(A,'VariableName',{'a','b','c','d'});
% Use case 1
% we just want to eliminate rows where variable c has a zero entry
% find row numbers where variable c has a zero entry
hasZero = tblA.c == 0; % logical indices
% make new table in which any row that has a zero entry in third column is
% removed (just keep the rows that do not have zero entries)
tblB = tblA(~hasZero,:);
% display original and final table
disp('Use Case 1')
disp('original table')
disp(tblA)
disp('revised table - rows where tblA.c has zero entries are removed')
disp(tblB)
% Use case 2
% we want to eliminate rows where any variable has a zero entry
% get the values that are in the table into an array so we can more easily
% work with them
tblVals = tblA{:,:}; % extract all elements into table using curly braces
% find any row that has a zero entry
hasZero = any(tblVals==0,2); % second argument used to find rows where zero occurs
% just keep the rows which do not have any zeros
tblC = tblA(~hasZero,:);
% display original and final table
disp('Use Case 2')
disp('original table')
disp(tblA)
disp('revised table - rows where any variable has a zero entry are removed')
disp(tblC)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by