MATLAB Answers

how to compare two text file and sort the lines of one of them with respect of another one ?

4 views (last 30 days)
MoHa on 12 Feb 2020
Commented: MoHa on 13 Feb 2020
I have a text file which has 6 3d coordinates (6 lines 3 columns) (as a reference file). I have to measure these points again and save them in another text file. later i need both text file by correct sorting.
The problem is that sometimes some points cannot be measured. in this case the discipline points will be destroyed. and also one or two or 3 points less.
Question is;
- how can I change the discipline of the first file (reference file) as well as the second file (measured file)? that means points 1 should be in the same line in both files (no matter which) and points that are not measured e.g. Point 2 should also be deleted from the reference file. these are my two files below:
measured: (points 2 and 4 could not be measured)
1.006253e + 03 9.822302e + 02 1.161773e + 01
1.006552e + 03 9.987608e + 02 1.161969e + 01
9.946106e + 02 1.012398e + 03 1.161471e + 01
9.989777e + 02 9.972292e + 02 1.161314e + 01
reference file:
1006.249 982.228 11.618
1010.194 984.407 11.617
1006.546 998 761 11 620
1002.063 1012.767 11.614
994,606 1012,399 11,616
998,971 997,229 11,614
thank you for your help.


Guillaume on 12 Feb 2020
It's fairly simple to do, use ismember or more likely ismembertol to find which rows of the reference file are in the measurement file (of course, after having imported both files with dlmread for example).
However, note that the values in your example files are not exactly identical, therefore what tolerance do you want to use for two different numbers to be considered equal?
MoHa on 13 Feb 2020
Hi Guillaume,
thanks für your answer. in every measurement the tolerance could be to one cm in (xyz).

Sign in to comment.

Accepted Answer

Guillaume on 13 Feb 2020
Edited: Guillaume on 13 Feb 2020
So, it will go something like this:](
%read the files. Not enough details given to give exact syntax. Something like this should work:
measurement = dlmread('C:\somewhere\measurementfile');
reference = dlmread('C:\somewhere\referencefile');
%find which rows of reference are within the range of the rows of measurement, and keep only these
rowstokeep = ismembertol(reference, measurement, 1, 'ByRows', true, 'DataScale', 1);
newreference = reference(rowstokeep, :);
%save into new file
dlmwrite('C:\somewhere\newreferencefile', newreference);
You may have to play around with the tol input of ismembertol (3rd input) and the DataScale input (if you want absolute tolerances, if you want relative tolerances, get rid of DataScale).


MoHa on 13 Feb 2020
i have implemented it. My measurement file dosnt have the second measuremen. i gave a tol of 0.01. i've got following:
rowstokeep =
6×3 logical array
1 1 1
0 0 1
1 1 1
1 1 1
1 1 1
1 1 1
Index exceeds matrix dimensions.
Error in meas_ref_2helmert (line 12)
newreference = reference(rowstokeep, :)
my question are:
- why the third element of second row shows logic 1 (i deleted the complete of second row)?
- the Error
thanks again für your help
Guillaume on 13 Feb 2020
Oops, I completely forgot to write the 'ByRows', true option of ismember. I've fixed the code now. rowstokeep will now be a 6x1 logical array indicating which rows of reference match rows of measurement.

Sign in to comment.

More Answers (0)