Reading .txt files and replace numbers from one file to another
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Geoff Hayes
il 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.
Risposta accettata
Geoff Hayes
il 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 Commenti
Geoff Hayes
il 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...
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Search Path in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!