How to read in a text file that has two different formats.

3 ビュー (過去 30 日間)
Kevin
Kevin 2014 年 8 月 26 日
回答済み: Kevin 2014 年 8 月 27 日
How can I read in a text file that does not have a consistent format for every row of data?
Here is an exert of the file I need to read.
2014-08-05 08:56:09.936: From 10.10.30.3: bytes=60 seq=06bf ID=48f6 time=32.110ms
2014-08-05 08:56:10.435: From 10.10.30.3: bytes=60 seq=06c0 ID=48f7 time=32.160ms
2014-08-05 08:56:10.903: Timeout waiting for seq=06bc
2014-08-05 08:56:10.934: From 10.10.30.3: bytes=60 seq=06c1 ID=48f8 time=32.475ms
Notice that the third row is not like the others. The following code works when I don't have these occasional rows with the "Timeout" message.
fid=fopen('ping.txt')
PingFile=textscan(fid, '%s %s From%s bytes=%d seq=%s ID=%s time=%f %*[^\n]')
How do I capture the information in these occasional rows, and read in the entire file?
  2 件のコメント
José-Luis
José-Luis 2014 年 8 月 26 日
Will the message always be: "Timeout waiting..."?
Kevin
Kevin 2014 年 8 月 27 日
Yes, the message will always be "Timeout waiting for seq=", followed by a hexadesimal number.

サインインしてコメントする。

採用された回答

Kevin
Kevin 2014 年 8 月 27 日
Final conclustion based on the answers from dbp and Haderlein. Thank you both for your help.
AllData=textread('ping.txt','%s','delimiter','\n');
IsPing=cellfun(@isempty,strfind(AllData,'Timeout'));
PingData =AllData(IsPing);
TimeOut = AllData(~IsPing);
PDfn=@(s) textscan(s, '%s %s From%s bytes=%d seq=%s ID=%s time=%fms');
TOfn=@(s) textscan(s, '%s %s Timeout waiting for seq=%s');
PingCell=cellfun(PDfn,PingData,'uniformoutput',false);
ToutCell=cellfun(TOfn,TimeOut,'uniformoutput',false);

その他の回答 (1 件)

dpb
dpb 2014 年 8 月 26 日
I'd do it a little differently...bring the file into memory and eliminate the bum lines, then scan from memory.
>> file=textread('kevin.txt','%s','delimiter','\n'); % textread is handy sometimes, too...
>> file=file(cellfun(@isempty,strfind(file,'Timeout'))) % get rid of timeout lines
file =
'2014-08-05 08:56:09.936: From 10.10.30.3: bytes=60 seq=06bf ID=48f6 time=32.110ms'
'2014-08-05 08:56:10.435: From 10.10.30.3: bytes=60 seq=06c0 ID=48f7 time=32.160ms'
'2014-08-05 08:56:10.934: From 10.10.30.3: bytes=60 seq=06c1 ID=48f8 time=32.475ms'
Nos create a function handle for the scanning...
>> fn=@(s) textscan(s, '%s %s From%s bytes=%d seq=%s ID=%s time=%fms')
fn =
@(s)textscan(s,'%s %s From%s bytes=%d seq=%s ID=%s time=%fms')
>> cellfun(fn,file,'uniformoutput',false)
ans =
{1x7 cell}
{1x7 cell}
{1x7 cell}
  2 件のコメント
Kevin
Kevin 2014 年 8 月 27 日
Thank you dpb. This works very well. I do, however, want to retain the information in the "Timeout" rows. I don't know anything about using the "@" simble, so I'm not sure how to modify your code.
Michael Haderlein
Michael Haderlein 2014 年 8 月 27 日
Just modify the following way:
isdata=cellfun(@isempty,strfind(file,'Timeout'));
data=file(isdata);
timeout=file(~isdata);
Then apply the fn function on data instead of file. The timeout information can be read out by similar means.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by