필터 지우기
필터 지우기

How to read a .txt file from a certain keyword onwards

조회 수: 5 (최근 30일)
Yaameen
Yaameen 2015년 5월 14일
편집: Yaameen 2015년 5월 26일
[13/05/15 - 15:52:49:848] RXCLIENT,2,3,1,254,-68,87,1121651473,
[13/05/15 - 15:52:49:858] TXCLIENT,2,1,1121655561,1,
[13/05/15 - 15:52:51:818] RXCLIENT,2,3,2,250,-67,90,1153331395,
[13/05/15 - 15:52:51:838] TXCLIENT,2,2,1153335490,1,
I have the above format .txt file which I want to read. I would like to read it by making RXCLIENT and TXCLIENT as starting points or keywords. In the end I would like it to be read as something like this.
A = [ RXCLIENT 2 3 1 254 -68 87 1121651473
RXCLIENT 2 3 2 250 -67 90 1153331395 ]
B = [ TXCLIENT 2 1 1121655561 1
TXCLIENT 2 2 1153335490 1 ]
Thanks
  댓글 수: 6
Image Analyst
Image Analyst 2015년 5월 15일
Why not make it easy for people to help you and attach your junk.txt file?
Yaameen
Yaameen 2015년 5월 15일
This is the junk file. My objective is to plot the numerical values again one another. This file is junk from a packet network link measurement

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

채택된 답변

Stephen23
Stephen23 2015년 5월 15일
편집: Stephen23 2015년 5월 15일
This can be done using textscan, first to read the file, and then to parse the strings:
% Read file:
fid = fopen('Client_171.txt','rt');
C = textscan(fid,'[%s-%s%[^\n]','HeaderLines',1);
fclose(fid);
% RX parsing
idx = strncmp(C{3},'RXCLIENT,',9);
str = sprintf('%s\n',C{3}{idx});
fmt = ['RXCLIENT',repmat('%d64',1,7)];
A = cell2mat(textscan(str,fmt,'Delimiter',','));
% TX parsing
idx = strncmp(C{3},'TXCLIENT,',9);
str = sprintf('%s\n',C{3}{idx});
fmt = ['TXCLIENT',repmat('%d64',1,4)];
B = cell2mat(textscan(str,fmt,'Delimiter',','));
And we can view the output arrays in the command window:
>> A(1:5,:)
ans =
1 1 0 248 -73 72 3411023465
2 3 1 250 -73 72 3461904538
2 3 2 248 -74 69 3494064440
2 3 3 255 -74 69 3525984367
2 3 4 252 -76 63 3558144375
>> B(1:5,:)
ans =
2 1 3461908922 1
2 2 3494068828 1
2 3 3525988511 1
2 4 3558148502 1
2 5 3590548697 1
Converting the date and time is easy, for both TX and RX this code can be used:
>> tmp = strcat(C{1}(idx),'_',C{2}(idx));
>> dtv = datevec(tmp,'dd/mm/yy_HH:MM:SS:FFF');
>> uint32(dtv(1:5,:))
ans =
2015 5 13 10 55 23
2015 5 13 10 55 25
2015 5 13 10 55 27
2015 5 13 10 55 29
2015 5 13 10 55 31
  댓글 수: 5
Stephen23
Stephen23 2015년 5월 26일
편집: Stephen23 2015년 5월 26일
Interesting. Thank you for providing an example, I will have a think about it and get back to you.
Yaameen
Yaameen 2015년 5월 26일
편집: Yaameen 2015년 5월 26일
@Stephen Cobeldick: I was thinking about it and maybe it can be done as following. Im quite new to word parsing in Matlab so not sure if its possible. Anyway, my idea is reading the whole file as a string, then use a format to identify the particular string (ex: RXCLIENT,%d,%d,%d,%d) and store it in {row x 1} cell array. Then convert to a matrix.

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

추가 답변 (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