how can i delete unnecessary rows in a dataset?
3 ビュー (過去 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!