How to read the header lines in a text file?
Show older comments
I have a text file with an header lines contains names and numbers. There is a columned data is there below the header lines. how to read the header line? also i wants to read the data separately also? how to write commands for it?
5 Comments
Is the data not in some standard defined format? Format specifications for data that is expected to be read in by some other program usually includes strict rules for how many header lines there are or some special character that either represents a header line or the end of the header/start of the data
dpb
on 25 Sep 2014
Need an example of the file to have any specific formatting help; in general
doc textscan
dpb
on 26 Sep 2014
You're not going out of your way to make things easy for somebody who has no klew about the problem...answers/help can only be as good as the input provided.
How many physical records are actually in the above and where, specifically are the record beginnings/endings? What, other than the two obvious date/times are the other values of interest? It could be presumed the place is the first string, perhaps, or is it some set of coordinates from the other numeric values? Another guess is that, perhaps the final "02" on the end of the first (or second record if there is a break as shown) is the number of channels in the file? Do you have to deal with more than the present case of 2?
There are examples in
doc textscan
of reading mixed data formats
Adam
on 26 Sep 2014
If you really want some "fun" you can use regexp to extract the information you want having read in a header, but your example still does not define exactly what a header is in order to separate it out from everything else when reading the data in.
I spent most of last week messing around with regexp for the first time to extract the information I wanted from such a text file, but I was working from a clearly defined specification document that contained an example file so that I could test my regular expressions against all the valid cases. Without that clear definition it is rather difficult!
Answers (1)
Sean de Wolski
on 25 Sep 2014
Also look at just using the import tool to import them as a cell array:
Home Tab -> Import Data -> Select your file
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!