Info
This question is closed. Reopen it to edit or answer.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to add data from a text file to another text file ?
1 view (last 30 days)
Show older comments
Hello, I want to modify File 2 to File 3 based on File 1. As you can see in the picture attached to this question, some (m,n) values in File 1 does not exist in File 2 so I want to add them with a correction = 0 so that I can get File3.
I tried to write File 2 in a cell and then write it back with the added lines but I don't know how to do it correctly, Here is the first lines of my code if someone can help
mcorrection = str2double(C{1,1});
ncorrection = str2double(C{1,2});
correction = C{1,3};
data=readfile(name);
HeaderLines = 8;
for i=(HeaderLines+1):numel(data)
str=data{i};
vals=textscan(str,' %f %f %f %f %f %f %f %f');
m=vals{1}; n=vals{2};
for j=1:length(correction)
if m == mcorrection(j) && n == ncorrection(j)
A{j} = sprintf('%d %d %f',str2num(C{1,1}{j}),str2num(C{1,2}{j}),str2num(C{1,3}{j}));
else
A{j} = sprintf('%d %d 0.0',str2num(C{1,1}{j}),str2num(C{1,2}{j}));
16 Comments
Rik
on 6 Oct 2020
Edited: Rik
on 6 Oct 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your code more readable.
Don't you want to match the n and m positions for the corrections? And repeating your headers in the table will make it much more difficult to read the file later on.
I would suggest to read all the data, modify your data inside Matlab, and then write the entire file again. Then you can also decide what you want to do when an n,m pair is in both of your correction files.
Izem
on 6 Oct 2020
I apologize for that, I forgot to do it.
I wanted to match n and m and I wrote a code that can do that. now sometimes a couple(m,n) can exist in File 1 and be absent in File2 so I want to add it to this file with a correction = 0 just like I explained in the picture.
So what I tried to do is, for every n and m in File 1 I run throught the File 2 and if I find it I write the line into a cell and if not I create a new line and write to a cell to in order to write back the entire File 2 with the new lines, The problem is that I am blocked in the second step which is creating the none existing (n,m) especially that in File 1 I have 10 lines that have the same (m,n) can you please write matlab lines to do that ?
Rik
on 6 Oct 2020
Split it into steps. If you can't solve a problem in one go, don't give up and ask me to write it for you. Try to split your problem into solvable parts.
You already have the line in your original file. You need to change some of them to include data based on other files. That is a single problem. Since m and n are going to match, you don't need to write those to your new file. How are you going to determine what the value of correction should be?
Make sure you have a value for correction for every line in your data, otherwise the reading of the created file will be a pain. If you can't think of a way to write the code, image how the person writing the reading code will feel.
Izem
on 6 Oct 2020
Edited: Rik
on 6 Oct 2020
'How are you going to determine what the value of correction should be?'
As I wrote in my post, the correction for the new added lines will be a zero, I made a mistake though. It should be since C does not contain those value of m and n.
A{j} = sprintf('%d %d 0.0',m,n);
'You already have the line in your original file. You need to change some of them to include data based on other files'
I think you didn't get my problem. I don't need to change any already existing line in File2, I need to add new lines with (m,n) that exist in File1.
'Since m and n are going to match, you don't need to write those to your new file'
Then how I am gonna modify File 2 if I don't write all the lines in a cell and then wrrite it back ?
Rik
on 6 Oct 2020
Exactly. You don't want to modify file2, you want to modify file1 so it also contains the information in file2 and file3.
You should only add a single column: correction. Your only task for this problem is figuring out the value of correction for that specific line. So you start with this:
0 1 1 0.0000000000e+00 4.6000000000e+01
What is the value of correction?
You need to look up the value in file2 and file3 for this combination of n,m (and also Numero, X and Y?). If a value exists in both you need to decide what the value of correction should be.
Izem
on 6 Oct 2020
Now I am sure I didn't explain well what I want because it has nothing to do with what you said.
I don't want to modify File1, it is good.
The correction will not be used in File1 in any case. so File 1 is no gonna change
I want to modify File2 to get File3 as I explained in the picture and in my previous comments.
I want to use File1 just to get (m,n) that does not exist in File2 and then give them correction =0 to get File 3 from File2 with all the (m,n) as in File1.
(I requested weeks ago to change File1, this new request has nothig to do with that )
Is that clear ?
Rik
on 6 Oct 2020
I think I understand now. In that case you need to load all combinations of n and m in file1. Then you can go through that list to write file3. If the combination is present in file2 you use that number, if it is missing you put in 0.
The conclusion is still similar: you need to load files completely to Matlab arrays, so you can more easily work with them.
Izem
on 6 Oct 2020
Great ! But I understand perfectly your idea and I had others but my problem is to write it in Matlab so can you please write something that works? I have already wasted enough time trying to do so..thank you so much !
(Numero does not go from 1 to 10 necessarily, It may go to 13, 35...)
Mario Malic
on 6 Oct 2020
"I have already wasted enough time trying to do so..." I believe this is called learning and no time is wasted by doing so. You can familiarise yourself better with MATLAB by Onramp tutorial and try to look for and ask information efficiently as these questions are quite common in here.
Rik
on 6 Oct 2020
You already have code that will read a file and get the values of n and m. What is your question?
You need to read file1 and file2 to Matlab variables. Show what you tried to achieve that. The code I gave you a while back (which you have modified for the code in this question) should get you most of the way there.
Don't hurry to the solution. Break everything up in steps you can handle. Have you opened readfile.m? Do you think I was able to write such a complex function in one go?
- Read the data from file1 to variables.
- Read the data from file2 to variables.
- Generate a masterlist of all n,m combinations.
- For each n,m combination, attempt to find the correction value from file2. If it is missing, assign 0.
- Write n,m,correction to file3.
Start with 1 and don't continue to the next one until you finished it.
Izem
on 6 Oct 2020
What do you think ?
HeaderLines = 8;
file1 = readfile('conf001_00630.00Hz.ray');
file2 = readfile('InputPidSidelinef630.dat');
A = [];
for i=(HeaderLines+1):numel(file1)
%store in temp variable
if isempty(file1{i}),continue,end
str=file1{i};
vals1=textscan(str1,' %f %f %f %f %f %f %f %f');
m1=vals1{1}; n1=vals1{2};
A(i-HeaderLines,1) = m1;
A(i-HeaderLines,2) = n1;
end
B = [];
for i=1:numel(file2)
%store in temp variable
if isempty(file2{i}),continue,end
str2=file2{i};
vals2=textscan(str2,' %f %f %f');
m2=vals2{1}; n2=vals2{2};
B(i,1) = m2;
B(i,2) = n2;
B(i,2) = vals2{3};
end
for i=1:length(A)
for j=1:length(B)
if A(i,1)== B(j,1) && A(i,2)== B(j,2)
A(i,3) = B(j,3);
else
A(i,3) = 0;
end
end
end
fid = fopen('file3.dat','w');
for i = 1:length(A)
fprintf(fid,'%d %d %f',A(i,1),A(i,2),A(i,3));
end
fclose(fid);
Rik
on 6 Oct 2020
Does it do what you want? Your last loop (prior to writing the file) looks different from what I understood you wanted, but if the results are what you need, then that is fine.
Izem
on 7 Oct 2020
Hi, I made some corrections and now it does what I want, can we optimize it ?
HeaderLines = 8;
file1 = readfile('conf001_00630.00Hz.ray');
file2 = readfile('InputPidSidelinef630.dat');
A = [];
for i=(HeaderLines+1):numel(file1)
%store in temp variable
if isempty(file1{i}),continue,end
str1=file1{i};
vals1=textscan(str1,' %f %f %f %f %f %f %f %f');
m1=vals1{1}; n1=vals1{2}; numero=vals1{3};
if numero == 1
A = [A;[m1 n1]];
end
end
B = [];
for i=1:numel(file2)
%store in temp variable
if isempty(file2{i}),continue,end
str2=file2{i};
vals2=textscan(str2,' %f %f %f');
m2=vals2{1}; n2=vals2{2}; correction=vals2{3};
B = [B;[m2 n2 correction]];
end
for i=1:length(A)
for j=1:length(B)
if A(i,1)== B(j,1) && A(i,2)== B(j,2)
A(i,3) = B(j,3);
break
else
A(i,3) = 0;
end
end
end
fid = fopen('file3.dat','w');
for i = 1:length(A)
fprintf(fid,'%d %d %f \n',A(i,1),A(i,2),A(i,3));
end
fclose(fid);
Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)