Clear Filters
Clear Filters

Skip header and comments within file

28 views (last 30 days)
Sara Principi
Sara Principi on 9 Jan 2018
Answered: Matt Sprague on 11 Jan 2018
Hi everybody, I am pretty new to Matlab.. I am trying to read a .dat file skipping the header and the comment lines within the file. The file looks like this.
# (EXCLUDING parts overlapped by quadrics)
#
# Dose units are eV/g per primary history
# Number of voxels in x
# 3 1 3
# Voxels size dx
# 1.00000E+01 1.00000E+00 1.00000E+01
# The granularity used to compute the voxels mass was:
# 10
#
# For plotting purposes
# the low end and the middle point of each voxel
#
# xLow(cm) : xMiddle(cm) : yLow(cm) : yMiddle(cm) : zLow(cm) : zMiddle(cm) : dose (eV/g) : +-2sigma : voxel mass (g) : Pure(+)/Overlapped(-)
# zVoxIndex=1
# yVoxIndex=1
0.00000E+00 5.00000E+00 0.00000E+00 5.00000E-01 0.00000E+00 5.00000E+00 2.52392E+00 4.9E-04 1.19000E+02 +
1.00000E+01 1.50000E+01 0.00000E+00 5.00000E-01 0.00000E+00 5.00000E+00 2.64684E+00 5.0E-04 1.19000E+02 +
2.00000E+01 2.50000E+01 0.00000E+00 5.00000E-01 0.00000E+00 5.00000E+00 2.52420E+00 4.9E-04 1.19000E+02 +
# zVoxIndex=2
# yVoxIndex=1
0.00000E+00 5.00000E+00 0.00000E+00 5.00000E-01 1.00000E+01 1.50000E+01 2.64757E+00 5.0E-04 1.19000E+02 +
1.00000E+01 1.50000E+01 0.00000E+00 5.00000E-01 1.00000E+01 1.50000E+01 2.65801E+00 1.6E-02 1.20480E-01 +
2.00000E+01 2.50000E+01 0.00000E+00 5.00000E-01 1.00000E+01 1.50000E+01 2.64726E+00 5.0E-04 1.19000E+02 +
# zVoxIndex=3
# yVoxIndex=1
0.00000E+00 5.00000E+00 0.00000E+00 5.00000E-01 2.00000E+01 2.50000E+01 2.52385E+00 4.9E-04 1.19000E+02 +
1.00000E+01 1.50000E+01 0.00000E+00 5.00000E-01 2.00000E+01 2.50000E+01 2.64743E+00 5.0E-04 1.19000E+02 +
2.00000E+01 2.50000E+01 0.00000E+00 5.00000E-01 2.00000E+01 2.50000E+01 2.52422E+00 4.9E-04 1.19000E+02 +
I would like to have only numbers. I have tried with this that works for the first three lines after the header (the first 19 lines) but I cannot manage to read the other lines without the comments.
FID=fopen('tallyVoxelDoseDistrib.dat');
datacell = textscan(FID, '%f%f%f%f%f%f%f%f%f%s', 3, 'HeaderLines', 19);
fclose(FID);
Thank you very much in advance for your help!

Answers (1)

Matt Sprague
Matt Sprague on 11 Jan 2018
Since the header and comments all start with '#', you can set that character as the comment style to skip those lines and only read in the numerical data.
datacell = textscan(FID, '%f%f%f%f%f%f%f%f%f%s','CommentStyle','#');

Community Treasure Hunt

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

Start Hunting!