Reading .txt files and replace numbers from one file to another
15 views (last 30 days)
Show older comments
Hello, I have a problem with a code.
First of all I have two .txt files, file1.txt and file2.txt.
File1 has 2 columns with 3 lines, and File2 has 5 columns with 20 lines. I would like to read this files and after that I would like to take the two numbers of the first line of File1.txt and replace these two numbers to the column 3 and column 4 of the first line of the File2.txt.
I try to create a loop, but I can not understand how to create this code.
Could anyone help me?
2 Comments
Geoff Hayes
on 5 Apr 2020
Ivan - please post the code that you have written so that we can help you understand what isn't working with the code.
Accepted Answer
Geoff Hayes
on 5 Apr 2020
file1Data = importdata('file1.txt', ' ', 1); % skip the header row
file2Data = importdata('file2.txt');
Now you have two matrices and so you should be able to access the appropriate rows of either to do whatever changes are necessary. You can then write out the results back to file with writematrix or equivalent.
11 Comments
Geoff Hayes
on 6 Apr 2020
Is the line of File1.txt a string? Are the strings in each line of File2.txt the same length? Please illustrate with an example of what you want to do...
More Answers (0)
See Also
Categories
Find more on Search Path 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!