Ignoring header text when using dlmread or similar function
10 views (last 30 days)
Show older comments
Hi,
I have some data files in a format called .xvg. These files come from a program called Gromacs, which runs molecular dynamics simulations. A typical .xvg file looks like this:
# This file was created Tue May 31 09:51:38 2011
# by the following command:
# g_energy -f npt-md.edr -o analysis/pressure.xvg
#
# g_energy is part of G R O M A C S:
#
# GROningen MAchine for Chemical Simulations
#
@ title "Gromacs Energies"
@ xaxis label "Time (ps)"
@ yaxis label "(bar)"
@TYPE xy
@ view 0.15, 0.15, 0.75, 0.85
@ legend on
@ legend box on
@ legend loctype view
@ legend 0.78, 0.8
@ legend length 2
@ s0 legend "Pressure"
0.000000 270.490936
0.002000 217.628891
0.004000 246.423630
0.006000 231.255936
0.008000 227.903320
0.010000 170.745224
and the file continues on and on and on with these two columns of data. There is no text footnote, only a text header. Matlab is able to read .xvg files, since they are just files containing ASCII text.
I am able to use Matlab's ImportData function in the graphical user interface to import this data, specifying that there are a certain number of lines of text headers. This works, but importing many such files this way is rather slow, since one has to specify in the ImportData dialog the exact number of lines to ignore, and then one has to wait for Matlab to generate a preview before continuing.
My question is, is there a way to do this from the Command Window or using a script? I found that the function dlmread has one option,
M = dlmread(filename, delimiter, R, C)
where R and C specify the row and column of the upper left corner of the data. They are zero-based, the documentation says. So since my above example file has 19 lines of text header, it seems that I want to ignore rows 0 through 18. So I try:
M = dlmread('pressure.xvg', '', 19, 0)
and this works, and it works great! But, suppose I am lazy and do not wish to always manually count the number of lines of text header. Is there a way to have Matlab automatically ignore the lines of text header? The lines of text header always begin with either # or @ so perhaps there is a way to have Matlab ignore these lines?
Thank you very much for your time!
Andrew DeYoung
Carnegie Mellon University
0 Comments
Accepted Answer
Fangjun Jiang
on 31 May 2011
use textscan(), try the "CommentStyle" parameter. Hopefully, the .xvg file contains fixed number of #lines.
FID=fopen('test.xvg','rt');
a=textscan(FID,'%f%f','Headerlines',8,'CommentStyle','@');
fclose(FID);
a =
[6x1 double] [6x1 double]
>> a{1}
ans =
0
0.0020
0.0040
0.0060
0.0080
0.0100
1 Comment
Walter Roberson
on 3 Jun 2011
Unfortunately the user's file has # and @ indicating comments, and textscan() can only handle one comment symbol.
More Answers (1)
Sadia Khaf
on 26 Sep 2016
I have the same question for a different file format. I have an IONEX file, it can be read as a simple .txt file in MATLAB. You can see the attached file. There doesn't seem to be a specific comment style for this type of file and I can't count number of lines to ignore since I need to read multiple files.
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!