Identify column headers in a text file
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
fsgeek
am 26 Sep. 2015
Kommentiert: fsgeek
am 26 Sep. 2015
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 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 26 Sep. 2015
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!