MATLAB Answers

reading txt file and converting hex to decimal and plotting

53 views (last 30 days)
m j
m j on 16 Jul 2016
Commented: Walter Roberson on 30 Dec 2019
I need to convert a txt file that has nothing but hexadecimal to decimal and then plot the matrix.
I tried textread, fscan and textscan and continue to get errors.
M=textread('data.txt','%2c');
M=hex2dec(char(M));
n=4; % number of columns
M=reshape(M,n,[])'
I would like to use textread but cant figure out how to implement code that reads # of columns and assigns it to variable 'n'.

  5 Comments

Show 2 older comments
m j
m j on 17 Jul 2016
Later on I plan on removing the 34 lines of header. But first I would like to plot this data
dpb
dpb on 17 Jul 2016
That's the most confusing mishmash of stuff I think I've ever seen...what's actually supposed to be in the file and what isn't?
Looks like the record data must've been written as stream, not as character; if I look at the content of the above with a file viewer, it's not possible to infer just what the structure really was...from the last \n for a while it looks like:
0000 05E0 0D 0A 57 61 76 65 66 6F 72 6D 28 38 30 30 20 62 ..Waveform(800 b
0000 05F0 79 74 65 73 29 3A 20 54 68 65 73 65 20 76 61 6C ytes): These val
0000 0600 75 65 73 20 72 65 70 72 65 73 65 6E 74 20 74 68 ues represent th
0000 0610 65 20 76 61 72 69 6F 75 73 20 73 61 6D 70 6C 65 e various sample
0000 0620 20 76 61 6C 75 65 73 20 74 68 61 74 20 66 6F 72 values that for
0000 0630 6D 20 74 68 65 20 63 6F 6D 70 6C 65 74 65 20 45 m the complete E
0000 0640 4F 44 2E 53 61 6D 70 6C 65 20 54 69 6D 65 73 74 OD.Sample Timest
0000 0650 61 6D 70 28 34 20 62 79 74 65 73 29 3A 20 52 65 amp(4 bytes): Re
0000 0660 63 6F 72 64 73 20 68 6F 77 20 6D 61 6E 79 20 6D cords how many m
0000 0670 69 6C 6C 69 73 65 63 6F 6E 64 73 20 66 72 6F 6D illiseconds from
0000 0680 20 74 68 65 A9 64 00 00 F0 12 0F 20 F0 12 00 20 the⌐d..≡↕☼ ≡↕.
0000 0690 0B 00 06 00 A9 64 00 00 F0 12 0F 20 40 01 00 20 ..♠.⌐d..≡↕☼ @☺.
0000 06A0 0A 00 10 00 40 13 00 20 E6 FF 00 FF D9 63 00 00 ..►.@‼. µ . ┘c..
0000 06B0 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ...............
then after a bunch of zers, the next data section looks like:
0000 0CB0 00 00 00 00 69 06 00 00 00 00 00 00 0E 09 00 00 ....i♠......♫...
0000 0CC0 00 00 00 00 5B 08 00 00 00 00 00 00 08 08 00 00 ....[◘......◘◘..
0000 0CD0 00 00 00 00 D9 07 00 00 00 00 00 00 E6 09 00 00 ....┘•......µ...
0000 0CE0 00 00 00 00 05 0B 00 00 00 00 00 00 46 0C 00 00 ....♣.......F...
0000 0CF0 00 00 00 00 00 0D 00 00 00 00 00 00 C6 0C 00 00 ............╞...
0000 0D00 00 00 00 00 AF 0C 00 00 00 00 00 00 A2 0C 00 00 ....».......ó...
0000 0D10 00 00 00 00 40 0D 00 00 00 00 00 00 EA 0C 00 00 ....@.......Ω...
0000 0D20 00 00 00 00 62 0D 00 00 00 00 00 00 D6 0A 00 00 ....b.......╓...
0000 0D30 00 00 00 00 96 0B 00 00 00 00 00 00 FE 0B 00 00 ....û.......■...
0000 0D40 00 00 00 00 41 0C 00 00 00 00 00 00 5B 0C 00 00 ....A.......[...
0000 0D50 00 00 00 00 0D 0D 00 00 00 00 00 00 D2 0C 00 00 ............╥...
0000 0D60 00 00 00 00 54 0D 00 00 00 00 00 00 D2 0A 00 00 ....T.......╥...
0000 0D70 00 00 00 00 78 09 00 00 00 00 00 00 D6 0A 00 00 ....x.......╓...
0000 0D80 00 00 00 00 27 0C 00 00 00 00 00 00 FC 0C 00 00 ....'.......ⁿ...
0000 0D90 00 00 00 00 BD 0C 00 00 00 00 00 00 7A 0A 00 00 ....╜.......z...
0000 0DA0 00 00 00 00 44 09 00 00 00 00 00 00 5C 0B 00 00 ....D.......\...
0000 0DB0 00 00 00 00 6F 0C 00 00 00 00 00 00 83 0C 00 00 ....o.......â...
0000 0DC0 00 00 00 00 53 0A 00 00 00 00 00 00 53 0B 00 00 ....S.......S...
0000 0DD0 00 00 00 00 B3 09 00 00 00 00 00 00 F8 0A 00 00 ....│.......°...
0000 0DE0 00 00 00 00 A1 0B 00 00 00 00 00 00 A5 0C 00 00 ....í.......Ñ...
0000 0DF0 00 00 00 00 6D 0A 00 00 00 00 00 00 61 0B 00 00 ....m.......a...
0000 0E00 00 00 00 00 84 0C 00 00 00 00 00 00 5D 0A 00 00 ....ä.......]...
0000 0E10 00 00 00 00 2C 09 00 00 00 00 00 00 AD 0A 00 00 ....,.......¡...
0000 0E20 00 00 00 00 4A 09 00 00 00 00 00 00 BC 0A 00 00 ....J.......╝...
0000 0E30 00 00 00 00 54 09 00 00 00 00 00 00 94 08 00 00 ....T.......ö◘..
Really no klew what one is supposed to make of it, sorry... :(
PS. But, looks most likely was written as four channels little-endian 16-bit integers in channels 2 & 4. Or, of course, could be two channels written as 8-byte integers???
m j
m j on 18 Jul 2016
sorry I am new to writing a file thats machine read only, but I was trying to write my data as hex and then stream that to file. But seems I have done the opposite. Looking into writing it a character stream.

Sign in to comment.

Answers (2)

dpb
dpb on 16 Jul 2016
Edited: dpb on 17 Jul 2016
nCol=4; % number of columns
fid=fopen('data.txt','r');
M=fscanf(fid,'%2x',[nCol,inf]).';
fid=fclose(fid);
I can't for the life of me understand why TMW left the hex conversion out of textscan/textread but they did... :(
NB: Above presumes the file was written as '%02X'; ie, each value does indeed have two digits, not any leading blanks (altho not tested, I think it'll still work if there were a delimiter in that case, but not if there is no delimiter.

  2 Comments

m j
m j on 17 Jul 2016
I need a way to know how many columns are in the dataset, it varies for me
dpb
dpb on 17 Jul 2016
Well, looking at what you posted for a file, I can't make heads nor tails of it...looks right mucked up to me. See above comments.
There's nothing in that file approaching anything at all like what you describe above and certainly the description is incomprehensible. Is there the code that actually wrote it somewhere to read the source?; mayhaps that would explain the mysteries.

Sign in to comment.


Padmamalini  T H
Padmamalini T H on 29 Dec 2019
can someone help me as well. i have the same kind od ascii file. i imported them in a table and created a seperate table for timestamps and an individual table for each column of hex value. now i have to convert these hex columns to decimal and plot the output against their corresponding timestamps

  1 Comment

Walter Roberson
Walter Roberson on 30 Dec 2019
Nothing in the file that was posted in 2016 is in hex; there is a text header, and after that it is in binary.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by