How to delete specific lines from a txt file?

3 Ansichten (letzte 30 Tage)
Mario
Mario am 23 Okt. 2014
Bearbeitet: dpb am 23 Okt. 2014
Hello, this is my problem: Background: I am comparing 2 files that should match in the first column but one of the files skips some numbers, suppose (12,12), (13,13), (14,15)*, so until now I have use a code to find and display the mismatches on the screen and I manually delete the excess rows in the complete file and rerun the code until everything matchs. Problem: Now usually I have used this with small files not correcting more than 10 mismatches, the problem is that I have a big data set with over 1000 of mismatches, so I need a way to delete the complete row of the "leftovers" in the txt file. Next is shown my coding:
%r is the number of total rows
%the first part is made for fileA and fileB storing in E and G matrices
fid = fopen('fileA.txt');
num_ints = 23;
num_rows = r;
format = [repmat('%f ', 1, num_ints)];
G = textscan(fid, format, num_rows);
G = [G{1:num_ints}];
fclose(fid);
for a = 1:1:r
if (E(a,1)~=G(a,1));
disp(E(a,1));
break
end
end
disp ('End of search');
As always thank you for your help!

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 23 Okt. 2014
Mario - which of your two matrices, E or G, do you wish to delete the excess row from? Your code compares the first column from each as
if (E(a,1)~=G(a,1));
disp(E(a,1));
break
end
If you just wish to delete the row of E that fails the above condition, then why not try something like
for a = 1:1:r
if (E(a,1)~=G(a,1));
% remove row a from E
E(a,:) = [];
end
end
The above assumes that E is the matrix with excess rows that need to be deleted. For example
E = [ 1 2; 2 3; 4 4; 3 5; 4 4; 6 7; 5 9];
G = [ 1 3; 2 4; 3 8; 4 7; 5 2];
If we run the above code, then the third and sixth rows of E should be deleted.

Weitere Antworten (1)

dpb
dpb am 23 Okt. 2014
Bearbeitet: dpb am 23 Okt. 2014
Given your E and G arrays,
[~,idxE,idxG]=intersect(E(:,1),G(:,1));
write new files
E(idxE,:) and G(idxG,:)
NB: Above assumes integers as shown in example so exact comparison will be reliable.
doc intersect % and friends for gory details...

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by