Reading 3rd set of data from large text file

조회 수: 4 (최근 30일)
charles atlas
charles atlas 2011년 12월 5일
I have a large set of data that I am reading from a .lis file. There are 3 lines of header data, then a few thousand lines of numeric data in two columns. Then there are 5 lines of header data with numeric data below that in 9 columns. Finally there are 5 more lines of header data with 8 columns of numeric data below that.
I have a code that currently reads the first two columns of data. I am not sure however, how you would read the 3rd set of data. That is the 8 columns of data right below the 3rd set of header lines. Does anyone know how I would do this?
The .lis file looks like this:
Headerline
Headerline
Headerline
Column (of temp data), Column (of height data)
Headerline (x5)
9 columns of numeric data
Headerline (x5)
8 columns of numeric data
note: all of the data is seperated by spaces
  댓글 수: 2
Daniel Armyr
Daniel Armyr 2011년 12월 5일
Could you share the code you have to rad the first part of the data. Depending on which solution you have chosen for that, the solution for reading the rest of the data varies.
charles atlas
charles atlas 2011년 12월 5일
fid = fopen('data.lis','rt');
XX = textscan(fid,'%f%f','HeaderLines',3);
tempA=XX{:,1}; heightA=XX{:,2};

댓글을 달려면 로그인하십시오.

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 12월 5일
It can be done but it will largely depends on the actual data format, data length of your file. It's really hard to make it generic.
If this process is an one-time thing, maybe you can consider doing it through Excel. You can open the .lis file using Excel. It will go through a dialog. You can choose to use space as the delimiter.
Once the Excel file is created, you can use xlsread() to read all the data at once and then process it. See what are returned in the text and numerical data using [Num, Txt, Raw]=xlsread(). In the worst case, it's probably easier once you get all the data in the cell array Raw.
Or you can use Data=uiimport('MyData.xls'). Once you finish the dialog, you have the option to generate the code for the uiimport() processing that you've done.

charles atlas
charles atlas 2011년 12월 5일
Is there any way to have a code that cycles through the text file and says once you see numeric data is 8 columns (skipping the 9 columns of data section) then read that data into an array?)
or being that the 8 columns of data is the only data labeled as "HEIGHT PROFILE STATISTICS", there might be a code that would serch for that specific line of text, and then read the 8 columns of data 4 headerlines below it?
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 12월 5일
Yes, you could.
use line=fgetl() to read each line
use Data=textscan(line,'%f') to count how many numerical data in a line.
Walter Roberson
Walter Roberson 2011년 12월 5일
You could also try
Datafields = regexp(line, ' ', 'split');
length(Datafields) would then be the number of fields, and
str2double(DataFields) would be the numeric content of the fields.
In theory this should be faster than doing the numeric conversions and counting the results, but in practice it could go either way, as the scanning routines are quite optimized.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by