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

[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 Comments

I was able to parse the text when I removed [13/05/15 - 15:52:49:848] lines. But the problem is the text files is more than 1000 lines long. So is there a way I can start my textscan from TXCLIENT/RXCLIENT?
Current code
close all
clear all
fileID = fopen('Junk.txt');
D = textscan(fileID,'%s %u8 %u8 %u8 %u8 %d8 %u8 %u32','Delimiter',',');
fclose(fileID);
TxPacket.pkttype=D{2}(strcmp(D{1},'RXCLIENT'));
TxPacket.datalength=D{3}(strcmp(D{1},'RXCLIENT'));
TxPacket.pktnumber=D{4}(strcmp(D{1},'RXCLIENT'));
TxPacket.SQI=D{5}(strcmp(D{1},'RXCLIENT'));
TxPacket.RSSI=D{6}(strcmp(D{1},'RXCLIENT'));
TxPacket.LQI=D{7}(strcmp(D{1},'RXCLIENT'));
TxPacket.timestamp=D{8}(strcmp(D{1},'RXCLIENT'));
Do your strcmp(D{1},'RXCLIENT') once and store it in a variable and use it for the following lines.
I am sorry, but I don't understand what you mean. Can you give an example of what I should do to start text scan from RXCLIENT onwards on each line.
RXLines = strcmp(D{1}, 'RXCLIENT');
TxPacket.pkttype=D{2}(RXLines);
TxPacket.datalength=D{3}(RXLines);
TxPacket.pktnumber=D{4}(RXLines);
TxPacket.SQI=D{5}(RXLines);
TxPacket.RSSI=D{6}(RXLines);
TxPacket.LQI=D{7}(RXLines);
TxPacket.timestamp=D{8}(RXLines);
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

Sign in to comment.

 Accepted Answer

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 Comments

Can you explain to me how this string format work [%s-%s%[^\n], thanks again.
@Yaameen: The format string '[%s-%s%[^\n]' is made up of these parts:
  • [ a literal character to ignore (start of each line)
  • %s to save the following string (date)
  • - a literal character to ignore (date-time)
  • %s to save the following string (time)
  • |%[^
@Stephen Cobeldick: Sometimes there are incidents where a new line is not printed and it is like this
[13/05/15 - 11:55:56:178] RXCLIENT,2,3,8,176,-94,12,1574274786,TXCLIENT,2,8,1574278924,4,
I can manually correct the file by entering a new line. I was wondering if there is a way to do this in Matlab?
Interesting. Thank you for providing an example, I will have a think about it and get back to you.
@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.

Sign in to comment.

More Answers (0)

Products

Asked:

on 14 May 2015

Edited:

on 26 May 2015

Community Treasure Hunt

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

Start Hunting!