Textscanning and exporting several text files
Show older comments
Hi,
I'm trying to combine data from several text files (file1, file2, etc.) and export the data into one file (or matrix) after textscan loops and some basic calculation. I have only managed to export from the last loop (overwrites?). My code so far:
d = dir('*.txt');
for n=1:numel(d)
filepath=['..\matlab_testing\'];
filena=['file' num2str(n) '.txt'];
[filepath filena];
if exist([filepath filena])
fid=fopen([filepath filena],'rt');
out = [];
row = fgetl(fid);
data = textscan(fid, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f');
out = [out;cell2mat(data)];
end
idx1 =out(:,3) == 100;
h1 = out(idx1,:);
xlswrite('h_ka_txt.xls',h1,'klo1', 'A2'); %works, but contains data only from the last file
How to continue so that I have one matrix that contains data from all the files?
Also, is the better way to textscan than (fid, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f'), if file has several hundred columns?
Thank you, all the help is greatly appreciated, Liisa
Accepted Answer
More Answers (1)
Liisa
on 30 Jan 2012
0 votes
Categories
Find more on Standard File Formats 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!