MATLAB: How to delete specific lines from a txt file

delete rowsMATLABmismatches

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!

Best Answer

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.