how can i delete unnecessary rows in a dataset?

조회 수: 1 (최근 30일)
Nadeera Gunartna
Nadeera Gunartna 2017년 6월 7일
댓글: Jan 2017년 6월 7일
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

답변 (3개)

Guillaume
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'}), :) = []
That can easily be written back to a text file with writetable if required.
  댓글 수: 1
Jan
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
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
Nadeera Gunartna
Nadeera Gunartna 2017년 6월 7일
편집: Nadeera Gunartna 2017년 6월 7일
I tried it, but it gives me an error "Conversion to cell from char is not possible". would you mind helping to correct it
filename = 'G:\test1.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'} ; idx = cell(length(str),1) ; for i = 1:length(str) idx1 = strfind(dataArray,str{i}); idx{i} = find(not(cellfun('isempty', idx1))); end idx = cell2mat(idx); dataArray(idx) = [];
raw = repmat({''},length(dataArray{1}),length(dataArray)-1); for col=1:length(dataArray)-1 raw(1:length(dataArray{col}),col) = dataArray{col}; end
Jan
Jan 2017년 6월 7일
편집: Jan 2017년 6월 7일
Please format the code using the "{} Code" button. Then post the complete error message, such that we do not have to guess, where the problem occurres.
See my answer for some ideas.

댓글을 달려면 로그인하십시오.


Jan
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';

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by