I have the huge set of text files for eg: 'x1_001','x1_002','x1_003','x2_001','x2_002','x2_003','x3_001','x3_002','x3_003'. I want to merge the files 'x1_001','x2_001','x3_001' together. Similarly: 'x1_002','x2_002','x3_002' and so on.. Finally need to plot a graph for the merged files. How can this be done?
Asked
Active
Viewed 692 times
-1
-
Do you want to simply concatenate the files, or do you want the data from the different files sorted in some way (merged)? What OS are you using? – beaker May 08 '14 at 17:22
-
1Possible duplicate: http://stackoverflow.com/questions/10219140/how-to-merge-two-files-into-one-text-file – beaker May 08 '14 at 17:23
-
Check the question I linked above and see if the answer there solves your problem. – beaker May 08 '14 at 17:32
-
no. this is completely static. Cant' it be done in a dynamic way? – nik May 08 '14 at 19:03
1 Answers
3
You can open up one of the files, copy it line-by-line into a new file until you're done, then open the next file, copy it line-by-line, etc. until you've gone through all of them.
Let's say we have two files, test1.txt and test2.txt. The contents of test1.txt are
test 1 string 1
test 1 string 1
and the contents of test2.txt are
test2 string 1
test2 string 2
test2 string 3
then if you use the following code:
f_list = {'test1.txt', 'test2.txt'};
f_new = fopen('output.txt','w');
for i = 1:length(f_list)
f_old = fopen(f_list{i},'r');
f_line = fgetl(f_old);
while ischar(f_line)
fprintf(f_new,'%s',f_line);
fprintf(f_new,'\n');
f_line = fgetl(f_old);
end
fclose(f_old);
end
fclose(f_new);
You get the following in output.txt, which is (I think) what you're wanting:
test 1 string 1
test 1 string 2
test2 string 1
test2 string 2
test2 string 3
-
no not really. First i need to combine the data from the files as described and finally plot the data. – nik May 08 '14 at 19:12
-
2You should clarify what you mean by "merge" or "combine". And as for plotting, you have provided absolutely no information regarding what you need plotted or in what fashion. At the moment the only advice we can give you is to use `plot()`. – Chuck May 08 '14 at 19:22