large text file reading

3 views (last 30 days)
Tyler
Tyler on 26 Mar 2013
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 Comments
Mahdi
Mahdi on 26 Mar 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?
Tyler
Tyler on 26 Mar 2013
the pattern is...
Heading
data
heading
data
heading
data
heading
data
The goal would be to end up with for matrices which contain the 4 data sets so that i can arrange them in the excel file as need be. I am not sure how i should go about directing my program to read one data set and then stop at the next header and start over. Also the amount of data in each set is not consistent, this is my biggest problem.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Mar 2013
Edited: Image Analyst on 26 Mar 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);

More Answers (1)

Ahmed A. Selman
Ahmed A. Selman on 26 Mar 2013
Edited: Ahmed A. Selman on 26 Mar 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);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!