I need to format a text file

1 visualización (últimos 30 días)
Duane Melvin
Duane Melvin el 24 de Mayo de 2022
Comentada: Voss el 25 de Mayo de 2022
0.6876 1 0CF02A83x Rx d 8 13 7D 00 7D 29 7D C0 FF
0.6882 1 0CFFD183x Rx d 8 1C DE 8F FE FA BD 8A 42
0.6976 1 0CF02A83x Rx d 8 0B 7D 08 7D 2F 7D C0 FF
0.6982 1 0CFFD183x Rx d 8 2B DE CF FB FA E1 8A 42
0.7076 1 0CF02A83x Rx d 8 FD 7C 0D 7D 29 7D C0 FF
0.7082 1 0CFFD183x Rx d 8 3C DE 8F FC FA AD 8A 42
0.7176 1 0CF02A83x Rx d 8 F9 7C 15 7D 25 7D C0 FF
0.7182 1 0CFFD183x Rx d 8 48 DE 2F FE FA 55 8A 42
This is a .txt file with potentially 500000 rows, only 8 are displayed for my question
I want to write a Matlab script to perform these 5 steps on the .txt file above:
1) remove every row that contains F02A
2) remove these items from the remaining rows 1 0CFFD183x Rx d 8 leaving only the 8 HEX values
3) keep only the first two bytes of the HEX values as in rows 2, 4, 6, 8, want to have only 1C DE remaining
example:
1C DE
2B DE
3C DE
48 DE
5) last step is to convert to Decimal, then perform this math
example for row one (DE*256)+1C)/DE or ((222*256)+28)/222 =
example for row two (DE*256)+2B)/DE or ((222*256)+43)/222 =
which produces this for first and second rows above etc
256.1261
256.1936
.
.
.
Thanks for answering

Respuesta aceptada

Voss
Voss el 24 de Mayo de 2022
It's not clear whether you need any of those intermediate results or just the final result, but here's one way to do everything:
fid = fopen('test.txt');
data = fread(fid,'*char').';
fclose(fid);
% Steps 1-3
hex_data = regexp(strsplit(data,newline()), ...
'([0-9A-F]+)x\>.*?(\<[0-9A-F]{2}\>)\s(\<[0-9A-F]{2}\>)', ...
'tokens','once');
hex_data = vertcat(hex_data{:})
hex_data = 8×3 cell array
{'0CF02A83'} {'13'} {'7D'} {'0CFFD183'} {'1C'} {'DE'} {'0CF02A83'} {'0B'} {'7D'} {'0CFFD183'} {'2B'} {'DE'} {'0CF02A83'} {'FD'} {'7C'} {'0CFFD183'} {'3C'} {'DE'} {'0CF02A83'} {'F9'} {'7C'} {'0CFFD183'} {'48'} {'DE'}
hex_data(contains(hex_data(:,1),'F02A'),:) = []
hex_data = 4×3 cell array
{'0CFFD183'} {'1C'} {'DE'} {'0CFFD183'} {'2B'} {'DE'} {'0CFFD183'} {'3C'} {'DE'} {'0CFFD183'} {'48'} {'DE'}
% Step 5
result = hex2dec(strcat(hex_data(:,3),hex_data(:,2)))./hex2dec(hex_data(:,3));
disp(result);
256.1261 256.1937 256.2703 256.3243
  4 comentarios
Duane Melvin
Duane Melvin el 25 de Mayo de 2022
thanks so much it is now functional
Voss
Voss el 25 de Mayo de 2022
Excellent!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by