Textscanning and exporting several text files
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
Liisa
am 30 Jan. 2012
0 Stimmen
Kategorien
Mehr zu Standard File Formats finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!