How to change a specific column to a .txt file by importing data from another .txt file.?

4 views (last 30 days)
Hello,
I have a question about a code. I have two .txt files (I am importing them in order to understand what the format is). Document1.txt has rows and columns of numbers, and "Document2.txt" has only one column with numbers. I would like to change the content of Document1.txt and creating 4 new documents.
The change that I want is to change the numbers of 4th column of "Document1.txt" (I mean the number "70") by the number of first row of column of "Document2.txt" (I mean number "30"). After this change , I would like to create a new .txt file with this change. (Let's call it "Document1_revised.txt"). After goinr to the second row of column of "Document2.txt" and doing the replacement etc..
To sum up I would like via a loop to create txt files by replacing specific thing from one file, and importing data from another file. I am importing the 4 .txt files that I would to create via commands in matlab.
Could anyone help me?
  3 Comments
Ivan Mich
Ivan Mich on 23 Apr 2020
Yes , but the problem is that I do not know how to compose the for loop. That's why I asked...
darova
darova on 23 Apr 2020
I tell you honestly: i don't want to read all this question, it's too long
Maybe a simple example would help
for i = 1:4
str = sprintf('document %d.txt',i); % file name1 to read
A1 = readtable(str); % load file1
A2 = A1;
A2(:,3) = 2*A2(:,3); % make some change to data
writetable(A2,['new_' str]); % save data2 as new file
end

Sign in to comment.

Answers (0)

Categories

Find more on Environment and Settings 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!