MATLAB Answers

James
0

Reading/writing text file while excluding header

Asked by James
on 3 Apr 2013
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

Can you post the code that you tried?
Also, approximately how many rows do you have?
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.
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.

1 Answer

Answer by Walter Roberson
on 4 Apr 2013
 Accepted Answer

FID=fopen('test_quake.txt');
datacell = textscan(FID, '%f%f%f%f%f', 'HeaderLines', 4, 'CollectOutput', 1);
fclose(FID);
A = datacell{1};

  6 Comments

That version definitely supported CollectOutput; see http://www.mathworks.com/help/releases/R2011a/techdoc/ref/textscan.html
Please cross-check the spelling in case of a typo: CollectOutput
Also please use
which -all textscan
just in case you are getting some odd version of the file.
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.
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.