Identify column headers in a text file

22 次查看(过去 30 天)
Dear all,
I am reading data from a text file with the standard format:
FILE TITLE
Header-1 Header-2 Header-3 Header-4 Header-5
58.1 Inf 7.0000 0 0
59.1 Inf 7.0000 0 0
60.1 7.7090e+04 4.8870 1.297e-05 129.7
61.1 Inf 7.0000 0 0
In each file, there is one line for a file title, followed by a series of numerical data columns with a mixture of numerical formats (engineering, integer, inf etc.). Each data column has its own text header. The number of data columns is variable.
I would like to obtain each column header, preferably as a cell of strings, and then use strcmp() to confirm the identity of each column.
I have tried various methods such as textscan and fgetl. The closest I can get is a character array, but this does not distinguish individual column names:
fid = fopen('file.txt', 'r');
firstline = fgetl(fid);
secondline = fgetl(fid);
>> whos secondline
Name Size Bytes Class Attributes
l 1x104 208 char
Thanks in advance for the help!
Best regards,
Louis

采纳的回答

Geoff Hayes
Geoff Hayes 2015-9-26
Louis - why not try using importdata? Look for the example where there is a text file with column headers, and you should be able to do something similar
A = importdata('myFile.txt','\t',1);
The above assumes that all columns in your file are tab-delimited and that there is only one header row. A should be a structure with a field for the data and a field cell array of column headers.
  1 个评论
fsgeek
fsgeek 2015-9-26
Hi Geoff,
This works perfectly. I'm amazed I didn't see this sooner. Thanks very much!
Louis

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by