Writing mutiple data files in a single text file
Mostrar comentarios más antiguos
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
Respuestas (3)
Friedrich
el 12 de Jul. de 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
Andrei Bobrov
el 12 de Jul. de 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')
Ali Haider
el 13 de Jul. de 2011
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!