large text file reading
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I am currently working on a program that will read a large text file and then export it into an excel file in a different organization. My file i am trying to read contains information in this form...
Time Pz Fz Fy My Ra Fx
0.000 12.350 1062.0 -9.9 12.0 245.391 0.8
0.040 12.348 1062.3 -9.2 10.3 245.391 0.8
0.080 12.347 1062.7 -9.6 5.2 245.391 1.1
0.120 12.347 1062.3 -9.6 5.2 245.391 0.7
0.160 12.347 1061.6 -9.9 13.8 245.391 0.2
0.200 12.349 1061.6 -9.6 -5.2 245.391 -0.3
0.240 12.350 1061.6 -9.9 -1.7 245.391 -0.1
0.280 12.350 1062.0 -9.9 10.3 245.391 0.4
0.320 12.348 1062.3 -9.2 18.9 245.391 0.6
0.360 12.347 1062.7 -9.4 15.5 245.391 0.8
and then it repeats 4 times going down the page. what is the best way for me to make my program check for a new title line and then start reading again so that i can end up with 4 matrices full if this data.
2 comentarios
Mahdi
el 26 de Mzo. de 2013
When you say it repeats 4 times. Do you mean that the headings Time, Pz, Fz, etc... repeat after that? Or is it just the numbers?
Respuesta aceptada
Image Analyst
el 26 de Mzo. de 2013
Editada: Image Analyst
el 26 de Mzo. de 2013
Just use fopen(), then fgetl(). Use strfind() to look for 'Time'. If it's there, read another line (the blank line), and increment the index of the block of data (it will be either 1, 2, 3, or 4), and start reading lines again (they'll be the numbers now). Then use sscanf() to extract the numbers from the string into your data variable.
fid = fopen('test.txt');
textLine = fgetl(fid);
dataIndex = 0;
while ischar(textLine)
fprintf('Read in %s\n', textLine);
% Check for 'Time'
if strfind(lower(textLine), 'time') >= 1
dataIndex = dataIndex + 1
% Read the blank line.
textLine = fgetl(fid);
else
% It's a line of numbers.
oneRowOfNumbers = sscanf(textLine, '%f');
% Turn into row vector;
oneRowOfNumbers = oneRowOfNumbers';
% Now append this row of numbers to the appropriate variable.
end
% Read the next line.
textLine = fgetl(fid);
end
% Close the file.
fclose(fid);
0 comentarios
Más respuestas (1)
Ahmed A. Selman
el 26 de Mzo. de 2013
Editada: Ahmed A. Selman
el 26 de Mzo. de 2013
Try this and note to specify your filename input (FIN) and filename output (FOUT) properly, i.e., FOUT must be with extension (xls) or (xlsx), e.g., 1.xls
Filenm =input('Input your FIN File name : ','s'); % here input the filename FIN
file1=fopen (Filenm,'r');
FOUT=input('Input your FOUT File name : ','s'); % here input the filename FOUT
[Binp, dn1]=fscanf(file1,'%s', [7 1]); % here is the header only as a matrix
[Ainp,dn]=fscanf(file1,'%g', [7 inf]); % here is the rest (numeric) values. You have 7 cols so pay attention to change it otherwise.
[doesItWork,TheWarning]=xlswrite(FOUT,Ainp');
if doesItWork==1;
disp ('Passed-- FIN was read and FOUT was written.')
else
TheWarning
end
fclose(file1);
0 comentarios
Ver también
Categorías
Más información sobre Text Data Preparation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!