Skip header and comments within file

9 次查看(过去 30 天)
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!

回答(1 个)

Matt Sprague
Matt Sprague 2018-1-11
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','#');

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by