Writing mutiple data files in a single text file
3 views (last 30 days)
Show older comments
Hi:
I have more than 50 text files named as: "w48_0.txt". Each text file contains two columns of variables. first row in each file is text header.
I want to combine all 50 files in a single text file such that the first column in new file remains the same while second column of every file writes according to the file number 1,2,3...50.
After that I want to plot these data.
How can I check the data written in new text file is correct?
All the files are in the following format:
frequency pressure
3 12
5 10
10 20
. .
.
I want to keep the first column same in new file but want to add second column in new file from all the files.
Can anyone help me?
Haider
0 Comments
Answers (3)
Friedrich
on 12 Jul 2011
Hi,
I would read in all the files, e.g
fid = fopen('w48_0.txt','r');
C = textscan(fid, '%f %f','Headerlines',1)
C{1} is the frequency column and C{2} is the pressure. Collect them all from the differnt files and then write them back and plot them.
To get all files easily you can do:
folder_content = dir('C:\folder_with_files');
for i=1:numel(folder_content)
%is non folder, so its a file
if ~folder_content(i).isdir
fid = fopen(fullfile('C:\folder_with_files',a.name),'r');
C = textscan(fid, '%f %f','Headerlines',1);
%do with the data whatever you like
fclose(fid);
end
end
0 Comments
Andrei Bobrov
on 12 Jul 2011
out = [];
for i1 = 1:50
fid = fopen(['w' num2str(i1) '_0.txt'],'r');
C = textscan(fid,'%f %f','Headerlines',1);
out=[out;cell2mat(C)]
fclose(fid);
end
dlmwrite('newfile.txt', out,'delimiter', ' ','newline', 'pc')
0 Comments
See Also
Categories
Find more on Data Import and Export 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!