Compare values from two text files and replace if they match
Show older comments
I have a problem of comparing two text files and replace value in third column if values in two first column match.
I have to text files. Each with tree columns (with the same format).
Text file 1 (short example):
177.50 0.00 1
180.00 0.00 1
0.00 10.00 1
2.50 10.00 1
5.00 10.00 1
7.50 10.00 1
10.00 10.00 1
12.50 10.00 1
Txt file 2 (short example):
2.50 10.00 0.01177
2.50 20.00 0.00977
5.00 0.00 -0.01480
5.00 10.00 -0.01244
5.00 20.00 -0.00440
5.00 30.00 -0.00228
I want to compare values of the first and second column in Text file 1 with the values in first and second column in Text file 2. If both values matches I want to replace “1” in third column in Text file 1 by corresponding value from third column from Txt file 2. The final output will be modified Text file 1 where in third column will be “1” in rows “not present” in Txt file 2 and values from Txt file 2 – in rows which match to Txt file 2.
For presented example output Text file 1 should look like this:
177.50 0.00 1
180.00 0.00 1
0.00 10.00 1
2.50 10.00 0.01177 %“1” was replaced by 0.01177 because this value is in Text file 2 in row “2.50 10.00”
5.00 10.00 -0.01244 %“1” was replaced by -0.01244 because this value is in Text file 2 in row “5.00 10.00”
7.50 10.00 1
10.00 10.00 1
12.50 10.00 1
Would be great if someone could help me out! Thanks in advance!
1 Comment
Bob Thompson
on 9 Jul 2019
What does your script look like so far?
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!