Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Extract just the header from a file

Subject: Extract just the header from a file

From: Amir

Date: 9 Jul, 2008 23:02:04

Message: 1 of 5

Hi. I have files that are composed of several dozen lines
of text (the header) followed by several thousand lines of
numbers (the data); all the files are formatted like this.

I'd like to extract in just the header. Right now I'm
reading in the entire file with 'importdata' (and then
extracting what I want), but that takes a very long time
given how much numerical data there are. Is there a more
elegant solution?

Thanks!

Subject: Extract just the header from a file

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 9 Jul, 2008 23:57:34

Message: 2 of 5

In article <g53g1b$8dq$1@fred.mathworks.com>,
Amir <amirschricker.DELETE@DELETE.gmail.DELETE.com> wrote:
>Hi. I have files that are composed of several dozen lines
>of text (the header) followed by several thousand lines of
>numbers (the data); all the files are formatted like this.

>I'd like to extract in just the header.

Is the number of header lines constant? Or is it easily predictable
from information that appears on the first line or two?

Do all the header lines happen to start with the same sequence
of characters (e.g., a '#' symbol at the beginning of each)?

Is there a specific marker for the end of the headers?

Is there something about the numberic lines that would allow you
to figure, 100% certain, that you were into the numbers as
soon as you had examined one of them?

--
  "Beauty, like all other qualities presented to human experience,
  is relative; and the definition of it becomes unmeaning and
  useless in proportion to its abstractness." -- Walter Pater

Subject: Extract just the header from a file

From: Andres

Date: 10 Jul, 2008 07:20:09

Message: 3 of 5

"Amir " <amirschricker.DELETE@DELETE.gmail.DELETE.com>
wrote in message <g53g1b$8dq$1@fred.mathworks.com>...
> Hi. I have files that are composed of several dozen lines
> of text (the header) followed by several thousand lines
of
> numbers (the data); all the files are formatted like
this.
>
> I'd like to extract in just the header. Right now I'm
> reading in the entire file with 'importdata' (and then
> extracting what I want), but that takes a very long time
> given how much numerical data there are. Is there a more
> elegant solution?
>
> Thanks!

Maybe the automatic header line detection in txt2mat (file
exchange) is of use to you if it is difficult to apply one
of the methods Walter indicated. With

[A,ffn,nh,SR,hl] = txt2mat(filename,'RowRange',[1 1]);

you would read in one dummy data row of the file in "A" and
the header lines as a single string in "hl" without having
to read the whole file.
Besides, to use importdata is definitely one of the slower
ways to read in a file. If you find a self-made solution
according to Walter's hints, it should rather base on
textscan.

Hth
Andres

Subject: Extract just the header from a file

From: Rune Allnor

Date: 10 Jul, 2008 07:35:20

Message: 4 of 5

On 10 Jul, 01:02, "Amir "
<amirschricker.DEL...@DELETE.gmail.DELETE.com> wrote:
> Hi. I have files that are composed of several dozen lines
> of text (the header) followed by several thousand lines of
> numbers (the data); all the files are formatted like this.

If this is a well-defined format, the header format will either
be known in advance (i.e. how many lines in the header), or the
size of a dynamic header will be sepcified in a known position
in the file.

You need to find out which it is, and then if necessary parse
the header for the size. Once you know how the size of the
header, use FGETL to read the lines.

Rune

Subject: Extract just the header from a file

From: Carlos Adrian Vargas Aguilera

Date: 10 Jul, 2008 15:31:06

Message: 5 of 5

Is better with TEXTEREAD function instead of FGETL. An you
can specify how many lines to be readed.

You could check out my READLINE function at File Exchange.

Carlos Vargas

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us