I need to format a text file

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

 Accepted Answer

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 Comments

First and foremost thanks for taking the time to answer!!
Thanks!
this is result in command window - not certain where I am floundering
>>formatter
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in formatter (line 2)
data = fread(fid,'*char ').';
This is the attempt to run the code, I cc cv'd this directly from my Matlab script window :
fid = fopen('51722test1.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}\&gt ;)', ...
'tokens','once ');
hex_data = vertcat(hex_data {:})
hex_data(contains(hex_data(:,1),'F02A '),:) = []
% Step 5
result = hex2dec(strcat(hex_data(:,3),hex_data(:,2)))./hex2dec(hex_data(:,3));
disp(result);
% here are the first rows of the 51722test1.txt file I am using, same
% format as the original file in this question
0.6182 1 0CFFD183x Rx d 8 36 DE AF F9 FA 7D 8A 42
0.6276 1 0CF02A83x Rx d 8 16 7D 09 7D 15 7D C0 FF
0.6182 1 0CFFD183x Rx d 8 36 DE AF F9 FA 7D 8A 42
0.6276 1 0CF02A83x Rx d 8 16 7D 09 7D 15 7D C0 FF
0.6182 1 0CFFD183x Rx d 8 36 DE AF F9 FA 7D 8A 42
0.6276 1 0CF02A83x Rx d 8 16 7D 09 7D 15 7D C0 FF
0.6282 1 0CFFD183x Rx d 8 32 DE 8F F7 FA 51 8A 42
0.6376 1 0CF02A83x Rx d 8 19 7D 12 7D 11 7D C0 FF
0.6382 1 0CFFD183x Rx d 8 24 DE 4F F7 FA 45 8A 42
0.6476 1 0CF02A83x Rx d 8 0F 7D 17 7D 20 7D C0 FF
0.6482 1 0CFFD183x Rx d 8 13 DE 4F F8 FA 25 8A 42
0.6576 1 0CF02A83x Rx d 8 00 7D 1D 7D 2A 7D C0 FF
0.6582 1 0CFFD183x Rx d 8 1B DE 6F FC FA 29 8A 42
0.6676 1 0CF02A83x Rx d 8 00 7D 1A 7D 2C 7D C0 FF
0.6682 1 0CFFD183x Rx d 8 34 DE 8F 00 FB 69 8A 42
0.6776 1 0CF02A83x Rx d 8 0B 7D 09 7D 2B 7D C0 FF
0.6782 1 0CFFD183x Rx d 8 2C DE 6F 01 FB 7D 8A 42
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
0.7276 1 0CF02A83x Rx d 8 05 7D 19 7D 33 7D C0 FF
0.7282 1 0CFFD183x Rx d 8 42 DE AF 00 FB 0D 8A 42
0.7376 1 0CF02A83x Rx d 8 0C 7D 0C 7D 39 7D C0 FF
0.7382 1 0CFFD183x Rx d 8 37 DE AF 00 FB 51 8A 42
0.7476 1 0CF02A83x Rx d 8 06 7D 06 7D 39 7D C0 FF
0.7482 1 0CFFD183x Rx d 8 31 DE EF FC FA C1 8A 42
0.7576 1 0CF02A83x Rx d 8 FD 7C 04 7D 37 7D C0 FF
0.7582 1 0CFFD183x Rx d 8 34 DE 4F FA FA B5 8A 42
0.7676 1 0CF02A83x Rx d 8 06 7D 09 7D 31 7D C0 FF
0.7682 1 0CFFD183x Rx d 8 44 DE 0F FA FA 69 8A 42
0.7776 1 0CF02A83x Rx d 8 1B 7D 0E 7D 32 7D C0 FF
0.7782 1 0CFFD183x Rx d 8 42 DE CF F8 FA 59 8A 42
0.7876 1 0CF02A83x Rx d 8 1B 7D 10 7D 31 7D C0 FF
0.7882 1 0CFFD183x Rx d 8 34 DE 4F FA FA 91 8A 42
0.7976 1 0CF02A83x Rx d 8 0F 7D 14 7D 33 7D C0 FF
0.7982 1 0CFFD183x Rx d 8 28 DE 4F FC FA D1 8A 42
0.8076 1 0CF02A83x Rx d 8 09 7D 1A 7D 3B 7D C0 FF
Voss
Voss on 24 May 2022
Edited: Voss on 24 May 2022
You've got an extra space at the end of your file name there:
fid = fopen('51722test1.txt ')
Try it without that space:
fid = fopen('51722test1.txt')
or better yet, specify the full path to the file:
fn = 'C:\some\directory\51722test1.txt';
fid = fopen(fn);
That will fix the particular error you ran into, but actually there's also extra space in other places; it looks like some of the code I posted got modified when you copy/pasted it, e.g., ">" is replaced by "&gt ;".
I'm not sure why that happened, but try to copy and paste it exactly as it is in the answer.
thanks so much it is now functional
Excellent!

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

on 24 May 2022

Commented:

on 25 May 2022

Community Treasure Hunt

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

Start Hunting!