I need to format a text file

조회 수: 3 (최근 30일)
Duane Melvin
Duane Melvin 2022년 5월 24일
댓글: Voss 2022년 5월 25일
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
Voss
Voss 2022년 5월 25일
Excellent!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by