Identify column headers in a text file

조회 수: 53 (최근 30일)
fsgeek
fsgeek 2015년 9월 26일
댓글: fsgeek 2015년 9월 26일
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개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by