I need to format a text file

1 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-5-24
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 个评论
Duane Melvin
Duane Melvin 2022-5-25
thanks so much it is now functional

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by