how can i delete unnecessary rows in a dataset?

3 visualizaciones (últimos 30 días)
Nadeera Gunartna
Nadeera Gunartna el 7 de Jun. de 2017
Comentada: Jan el 7 de Jun. de 2017
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

Respuestas (3)

Guillaume
Guillaume el 7 de Jun. de 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'}), :) = []
That can easily be written back to a text file with writetable if required.
  1 comentario
Jan
Jan el 7 de Jun. de 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.

Iniciar sesión para comentar.


KSSV
KSSV el 7 de Jun. de 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 comentarios
Nadeera Gunartna
Nadeera Gunartna el 7 de Jun. de 2017
Editada: Nadeera Gunartna el 7 de Jun. de 2017
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 el 7 de Jun. de 2017
Editada: Jan el 7 de Jun. de 2017
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.

Iniciar sesión para comentar.


Jan
Jan el 7 de Jun. de 2017
Editada: Jan el 7 de Jun. de 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';

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by