how can i delete unnecessary rows in a dataset?
2 views (last 30 days)
Show older comments
Dear Colleagues, Herewith I am attaching my dataset. I want to delete the full rows that contain "GB","RW","AR", "AM" and "NB". I would be grateful, if somebody can help me with this. Thanks
0 Comments
Answers (3)
Guillaume
on 7 Jun 2017
A much simpler method
t = readtable('test1.txt'); %optionally give name to columns with 'VariableNames'
t(ismember(t{:, 1}, {'GB', 'RW', 'AR', 'AM', 'NB'}), :) = []
1 Comment
Jan
on 7 Jun 2017
readtable is so smart. Sometimes this scares me. How does it decide if I want to import a 12 as a string '12'. I know, the documentation clears this. Anyway, +1.
KSSV
on 7 Jun 2017
%%REad the file
fid1 = fopen('test1.txt') ;
S = textscan(fid1,'%s','delimiter','\n') ;
S = S{1} ;
fclose(fid1) ;
%%get lines which have the strings
str = {'GB','RW','AR', 'AM' 'NB'} ;
idx = cell(length(str),1) ;
for i = 1:length(str)
idx1 = strfind(S,str{i});
idx{i} = find(not(cellfun('isempty', idx1)));
end
idx = cell2mat(idx) ;
%%remove those lines
S(idx) = [] ;
%%write this to text file
fid2 = fopen('test.txt','wt') ;
fprintf(fid2,'%s\n',S{:});
fclose(fid2) ;
2 Comments
Jan
on 7 Jun 2017
Edited: Jan
on 7 Jun 2017
With some guessing:
filename = 'G:\Albania\SIC\DS1\RPM10.txt';
delimiter = ',:';
formatSpec = '%s%s%s%s%s%*s%*s%s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, ...
'ReturnOnError', false);
fclose(fileID);
str = {'GB','AB','RW','AR','AM','NB','NH'} ;
remove = ismember(dataArray{1}, str);
for k = 1:numel(dataArray)
dataArray{k}(remove) = [];
end
The part for importing the data looks far to complicated. What is the wanted result for e.g.
000656,000675,000722,000641,10:51:57.000
? Wouldn't it be easier to read this by:
delimiter = ',';
formatSpec = '%s %f %f %f %f %s';
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!