Identify column headers in a text file
65 次查看(过去 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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!