Why does IMPORTDATA or LOAD read in HEX files incorrectly in MATLAB?

7 visualizzazioni (ultimi 30 giorni)
I have a .HEX file containing these values:
0x0C50 0xDA82 0xFFFF 0xF3D2 0x3D60 0x803E
When I try to read this hex data using the "load" function and convert it to decimal on a Windows machine, the data values starting with '0xD' are read as '0xE' when converted to decimal. For example, I call
load('test.hex')
where "test.hex" is the file which contains the above hex data. The output of the statement is
3152 60034 65535 62434 15968 32830
However, the data should be
3152 55938 65535 62418 15712 32830
Why does MATLAB not import the data from a HEX file as expected?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 15 Mag 2024
Modificato: MathWorks Support Team il 4 Giu 2024
The ability to read ASCII-coded hex using "importdata" or "load" is not available in MATLAB. To work around this issue, use "fscanf" to read the file. For example, you can read "test.hex" using "fopen" to open the file and "fscanf" to scan the hexadecimal values:
>> fid = fopen('test.hex');
>> a = fscanf(fid,'%x %x %x %x %x %x');
>> fclose(fid);
 
Another workaround is to read in the data as text and then use the "hex2dec" function to transform the hexadecimal values into decimals:
>> hexStr = '3FF';
>> D = hex2dec(hexStr)
D = 1023

Più risposte (0)

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by