Why does IMPORTDATA or LOAD read in HEX files incorrectly in MATLAB?
8 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Edited: MathWorks Support Team
on 4 Jun 2024
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?
Accepted Answer
MathWorks Support Team
on 15 May 2024
Edited: MathWorks Support Team
on 4 Jun 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
0 Comments
More Answers (0)
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!