Hi,
I would like to read the data below the words 'Link Results'. What i would really like to know is how to get the line number in the text file that I'm trying to read that has 'Link Results'. After I get that I know how to read the data. so my question would be how to find that number.
Thanks!

댓글 수: 3

Cedric
Cedric 2013년 10월 27일
편집: Cedric 2013년 10월 27일
Copy/paste the whole line if the data is on the same line, or a chunk of the data if it is on multiple line, including the first line (with Link Results) and the delimiter for the end of the block. Also, is there just one block of data or are there multiple blocks?
Here is a piece of the text I would like to read
. . . 877 0.01 0.01 0.01 72.01 0.01 1047 0.01 0.01 0.01 72.01 0.01
************
Link Results
************
<<< Link 1301 >>>
-------------------------------------------------------------
Flow Velocity Depth Percent
Date Time CFS ft/sec feet Full
-------------------------------------------------------------
SEP-29-2010 00:15:00 0.000 0.000 0.000 0.0
SEP-29-2010 00:30:00 0.000 0.000 0.000 0.0
SEP-29-2010 00:45:00 0.000 0.000 0.000 0.0
.
.
.
I figured I could just get the line index where it reads 'Link Results' (which is 7063 in this case but changes with the other text files I need to run through) and add 8 to get to my data
Thanks!
Cedric
Cedric 2013년 10월 27일
And how does the table end? Is it the end of file or is there some other content afterwards?

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

 채택된 답변

Cedric
Cedric 2013년 10월 27일
편집: Cedric 2013년 10월 27일

0 개 추천

Here is one way, assuming that the block of data is the last content that you have in the file. If not, I can update the answer. As Walter is pointing out, we have to read the file from the beginning (here we read the full content) and match pattern(s). This solution does that using a a regular expression, which is quite concise, but you could implement a more basic loop which scans each line.
content = fileread( 'myFile.txt' ) ;
blocks = regexp( content, 'Link Results.*?Full.*?\-\s(.*)', 'tokens' ) ;
block = blocks{1}{1} ;
data = textscan( block, '%s %s %f %f %f %f' ) ;
After running this, you get..
>> data
data =
{3x1 cell} {3x1 cell} [3x1 double] [3x1 double] [3x1 double] [3x1 double]
>> data{1}
ans =
'SEP-29-2010'
'SEP-29-2010'
'SEP-29-2010'
>> data{2}
ans =
'00:15:00'
'00:30:00'
'00:45:00'
>> data{3}
ans =
0
0
0
etc. Then you can post-process this cell array to get e.g. a cell array of time stamps, and a numeric array of data..
timeStamps = arrayfun( @(r) [data{1}{r}, ' ', data{2}{r}], ...
1:numel(data{1}), 'UniformOutput', false ).' ;
dataNum = [data{3:end}] ;
which leads to:
>> timeStamps
timeStamps =
'SEP-29-2010 00:15:00'
'SEP-29-2010 00:30:00'
'SEP-29-2010 00:45:00'
>> dataNum
dataNum =
0 0 0 0
0 0 0 0
0 0 0 0

댓글 수: 4

Sergio
Sergio 2013년 10월 27일
Hey that actually worked but it also read the last block which is a couple of lines about the analysis and run time. Is there a way to stop reading when it reaches the last date-time of data or at a certain line number. I know my results like 200 lines long but would be neat to make this parameter a little bit more flexible. Thank you very much! Your help is greatly appreciated.
Cedric
Cedric 2013년 10월 27일
This is why I was asking you for information about what comes next. Could you copy/paste the end of the table plus the lines which come next?
This would be the end of the text file **** OCT-01-2010 23:30:00 15.833 1.067 0.819 1.7 OCT-01-2010 23:45:00 15.496 1.061 0.810 1.7 OCT-02-2010 00:00:00 15.175 1.055 0.801 1.7
Analysis begun on: Sun Oct 27 16:18:20 2013
Analysis ended on: Sun Oct 27 16:18:37 2013
Total elapsed time: 00:00:17
Cedric
Cedric 2013년 10월 27일
편집: Cedric 2013년 10월 27일
Ok, just update the pattern in the regexp call as follows:
blocks = regexp( content, 'Link Results.*?Full.*?\-\s(.*?)\s*Ana', ...
'tokens' ) ;

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 10월 27일

0 개 추천

There are no functions built into MATLAB, or to any of the operating systems that current MATLAB run on, that can tell you which line number of a file that you are positioned to. None of the operating systems supported have any inherent concept of "line number".
Therefore if you want to know which line number something is on, you need to start at the beginning of the file, read line by line, counting each as you go, until you find the pattern you are looking for.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2013년 10월 27일

편집:

2013년 10월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by