how can i delete unnecessary rows in a dataset?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (3개)
Guillaume
2017년 6월 7일
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
Jan
2017년 6월 7일
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
2017년 6월 7일
%%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
Jan
2017년 6월 7일
편집: Jan
2017년 6월 7일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!