Challanges representing hexadecimal values as "integer double" in uitable

In a uitable, I’m editing multiple cells and I want to use hexadecimal (integer) as the input value. For most cases this turns out perfectly fine. However, hexadecimal values ending with 'e' are causing me a headache. It seems that Matlab interprets the input as string and converts the hexadecimal input (0xXXXX, without quotation marks) using ‘str2double’ before any callbacks are triggered. ‘str2double’ uses ‘sscanf’ behind the scenes and in ‘str2double’, ‘sscanf’ is called with format specifier ‘%f’. This makes sense when ‘str2double’ tries to convert to a double value. When ‘sscanf’ is called with ‘%f’, it probably triggers on the trailing ‘e’ for scientific notation. There is no value proceeding ‘e’ and ‘str2double’ fails to read the entire char-array and returns NaN.
I am able to catch the NaN-value and re-evaluate the string using ‘myStr2double’, where I call ‘sscanf’ with format specifier ‘%i’. This works, but it seems like an unnecessary step as not doing this works for all hexadecimal values (I’ve tested) except for those ending with ‘e’.
Here are some sample code to illustrate the problem:
str2double('0x1234')
returns 4660 (as expected)
str2double('0x123e')
returns NaN.
Is there a better solution to this behaviour, or am I trying to do something that is just outside of the intention?
In ‘myStr2double’, i call:
[a,count,errmsg,nextindex] = sscanf(s,'%i',1)
to read the entire hexadecimal char-array.

3 comentarios

Just to clearify. The following code:
testMax = 2^8-1;
for i = 1:testMax
if isnan(str2double(sprintf('0x%02x',i)))
fprintf('0x%02X ',i)
end
end
returns:
0x0E 0x1E 0x2E 0x3E 0x4E 0x5E 0x6E 0x7E 0x8E 0x9E 0xAE 0xBE 0xCE 0xDE 0xEE 0xFE
Stephen23
Stephen23 el 18 de Dic. de 2021
Editada: Stephen23 el 18 de Dic. de 2021
"...am I trying to do something that is just outside of the intention?"
Yes. STR2DOUBLE is not documenented to convert hexadecimal numbers. For reliable conversion use functions which are designed and documented for converting hexadecimal, e.g. HEX2DEC, SSCANF.
Thank you for the input, Stephen. I just want to clarify. STR2DOUBLE is the default callback for cell-edits in a uitable, so what I am doing wrong is expecting everything to work fine using a uitable to enter numerical values by their hexadecimal representation. This might not allways be the case. Sometimes I want to enter the data as numerical integers. HEX2DEC expects text input. Matlab interprets 0x1234 (not ''0x1234') as an unsigned integer of appropriate size and thus SSCANF (with integer formatspecifier) does the job of juggeling numeric and hexadecimal integers. I handle this by setting a custom 'CellEditCallback' for my table.

Iniciar sesión para comentar.

Respuestas (1)

Maybe using hex2dec instead of str2double will work:
str2double('0x1234')
ans = 4660
str2double('0x123e')
ans = NaN
hex2dec('0x1234')
ans = 4660
hex2dec('0x123e')
ans = 4670

4 comentarios

Another option is to use SSCANF:
sscanf('0x1234','%x')
ans = 4660
sscanf('0x123e','%x')
ans = 4670
Thank you for your answear, Benjamin. I was somewhat not precise enough and somewhat misleading in my question. I'm entering the data as numerical values. Not as text as the examples i gave. Your suggestion solves this problem, but not my real problem. I want to enter data as either numerical integers (e.g. 1,2,3,4,100..) or as numerical hexadecimal values (0x12,0x1234,0x123e - not text). SSCANF expecting integers, handles both cases and this is the solution I've chosen.
preprocess to determine which conversion to apply.
Ahh, my last comment is also not correct. As the original post is a few months old, my comments was out of memory and not the actual implementation. Edits in the cells are interpreted as text (char array) , making HEX2DEC a viable option if I do the correct preprocessing of the text. However, SSCANF already handles this distinction.
From the documentation of SSCANF for format specifier %i:
The values determine the base:
  • The default is base 10.
  • If the initial digits are 0x or 0X, then the values are base 16 (hexadecimal).
  • If the initial digit is 0, then values are base 8 (octal).
Given the time, I will add a new answear and close this question later this week, though probably not on friday :-)

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 10 de Sept. de 2021

Comentada:

el 20 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by