Reading/writing text file while excluding header

25 views (last 30 days)
I'm new-ish to MATLAB, and until now I've only used it to perform Matrix Displacement Method analyses for structures classes.
I have large earthquake record files that I would like to read in. They have headers (4 lines) that I would like to omit, and 5 columns of acceleration data (the number of rows depends on the size of the earthquake record). Is there a way to read in this data and then write the array to a new text file? I've spent hours pouring over past forum topics, and I haven't had any luck with my attempts.
The closest I've come is to use importdata(), but the 'data' array returns dimensions that don't seem right (that, and I don't know how to access the elements of a structured array like 'data').
Hints would be much, much appreciated!
  5 Comments
James
James on 4 Apr 2013
Edited: Walter Roberson on 4 Apr 2013
Thanks for the responses so far.
My source files look like:
PEER STRONG MOTION DATABASE RECORD. PROCESSING BY PACIFIC ENGINEERING.
FRIULI 09/11/76 1631, BUIA, UP
ACCELERATION TIME HISTORY IN UNITS OF G. FILTER POINTS: HP=0.4 Hz LP=20.0 Hz
NPTS= 7769, DT= .00500 SEC
-.6556807E-06 -.6741810E-06 -.6559686E-06 -.6705472E-06 -.6498322E-06
-.6731312E-06 -.6715338E-06 -.6567325E-06 -.4942821E-06 -.2008082E-06
.2541901E-06 .7106125E-06 .1093553E-05 .1325778E-05 .1121705E-05
.1825764E-06 -.1467073E-05 -.3893214E-05 -.7018337E-05 -.1086156E-04
-.1515249E-04 -.1893733E-04 -.2106391E-04 -.2061759E-04 -.1650684E-04
-.8531587E-05 .1919265E-05 .1271986E-04 .2007339E-04 .2081788E-04
.1399449E-04 -.2441929E-06 -.2112721E-04 -.4612119E-04 -.7019579E-04
-.8699628E-04 -.9050326E-04 -.7702844E-04 -.4508828E-04 .3048132E-05
.6435106E-04 .1321023E-03 .1944479E-03 .2380203E-03 .2475398E-03
... and can have more than 600 rows.
I have tried the following:
FID=fopen('test_quake.txt');
fprintf(FID,'%f','headerLines',4);
A=fscanf(FID,'%f',[inf]);
fclose(FID);
and
filename = 'test_quake.txt';
delimiterIn = ' ';
headerlinesIn = 4;
A=importdata(filename,delimiterIn,headerlinesIn);
And as I mentioned before, I have tried implementing the textscan() command to no avail.
Walter Roberson
Walter Roberson on 4 Apr 2013
In the above there appears to be empty lines in the headers. If those empty lines are present in the file, then you need to include those in the count of header lines.
You should not be using fprintf() for an input file.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Apr 2013
FID=fopen('test_quake.txt');
datacell = textscan(FID, '%f%f%f%f%f', 'HeaderLines', 4, 'CollectOutput', 1);
fclose(FID);
A = datacell{1};
  6 Comments
James
James on 4 Apr 2013
built-in (/Applications/MATLAB_R2011a.app/toolbox/matlab/iofun/textscan)
Oddly enough, after clearing my command history and workspace, it worked (without changing anything about the original script). I made a few changed to suit my needs (like printing to a text file for the output of the array, and changing the format of the output). Thanks again for all your efforts!
While I have your attention, is it a relatively easy tweak to read the text row by row instead of column-wise?
Thanks again, sincerely.
Walter Roberson
Walter Roberson on 4 Apr 2013
FID=fopen('test_quake.txt');
for K = 1 : 4; fgetl(FID); end; %throw away 4 lines
while true
[A, count] = fscanf(FID,'%f', 5);
if count == 0; break; end %end of file
.... process the line ...
end
fclose(FID);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!