Reading ascii data with headers

7 views (last 30 days)
Hi,
I am trying to read an ascii file with importdata function. The separator is tabulator (\t).
Data_and_Headers = importdata(DataTxTfile, '\t');
All_Data = Data_and_Headers.data;
Titles = Data_and_Headers.textdata(1,:);
Units = Data_and_Headers.textdata(2,:);
There are 2 header's rows. The first row is the title and the second is the units. Numeric values starts at the third row. The reading of the numeric data and units works fine but title's reading is not working. I have 31 columns and all the column's titles are regrouped in the first cell while the remaining 30 are empty. It seems that the tabulator is not used for title row but works fine for the remaining.
Any suggestions?
Ascii file is attached.
Thanks for your help.
Martin

Accepted Answer

Stephen23
Stephen23 on 1 Apr 2015
Edited: Stephen23 on 1 Apr 2015
Why not just use fgetl and textscan instead to load the data:
fid = fopen('Acquisition.txt','rt');
hdr = strtrim(regexp(fgetl(fid),'\t','split'));
unt = strtrim(regexp(fgetl(fid),'\t','split'));
mat = cell2mat(textscan(fid,repmat('%f',1,numel(hdr))));
fclose(fid);
This imports the uploaded file and produces these variables:
>> hdr(:)
ans =
'Time'
'X Displ (abs)'
'Y Displ (abs)'
'Z Displ (abs)'
'RX Displ (abs)'
...
'ABS Act Z-NW Displacement'
'ABS Act Z-NW Force'
'ABS Act Z-SE Displacement'
>> unt(:)
ans =
'sec'
'mm'
'mm'
'mm'
'rad'
'rad'
'rad'
'kN'
...
'kN'
'mm'
'kN'
'ABS Act Z-SE Force'
'ABS Act Z-SW Displacement'
'ABS Act Z-SW Force'
and of course all 46 rows of the numeric data:
>> whos mat
Name Size Bytes Class Attributes
mat 46x31 11408 double
  1 Comment
Martin Leclerc
Martin Leclerc on 1 Apr 2015
Hi Stephen,
Thanks a lot, it works just fine!
Regards,
Martin

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!