Reading ascii data with headers

7 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2015-4-1
编辑:Stephen23 2015-4-1
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

更多回答(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