Borrar filtros
Borrar filtros

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

5 visualizaciones (últimos 30 días)
Yaameen
Yaameen el 14 de Mayo de 2015
Editada: Yaameen el 26 de Mayo de 2015
[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 comentarios
Image Analyst
Image Analyst el 15 de Mayo de 2015
Why not make it easy for people to help you and attach your junk.txt file?
Yaameen
Yaameen el 15 de Mayo de 2015
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

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 15 de Mayo de 2015
Editada: Stephen23 el 15 de Mayo de 2015
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 comentarios
Stephen23
Stephen23 el 26 de Mayo de 2015
Editada: Stephen23 el 26 de Mayo de 2015
Interesting. Thank you for providing an example, I will have a think about it and get back to you.
Yaameen
Yaameen el 26 de Mayo de 2015
Editada: Yaameen el 26 de Mayo de 2015
@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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by