How can I avoid doubles from char arithmetic in fixed-point codegen in MATLAB Coder R2025a?

I am generating fixed-point code using Fixed-Point Designer and MATLAB Coder R2025a. My code will be used on a small embedded microcontroller that does not support floating-point operations. One of the lines of code that is being translated as resulting in data of type
"double" rather than "char" is the following:
x(:) = (s(i) - '0');
where "s" is a character array.
What do I need to do to get the generated code to not use doubles in instances like this?

 Respuesta aceptada

To resolve this issue, explicitly construct unsigned integers
x(:) = (uint8(s(i)) - uint8('0'));
such that the arithmetic expression evaluates to a 8-bit unsigned integer in MATLAB rather than a double.
Please ensure that any expressions present in your generated code do not evaluate to doubles.
After making these changes, regenerate your code.

1 comentario

If you use
int16(s(i)) - int16('0')
then you can handle the possibility that the character is in position 0 through 47. The uint8(s(i)) - uint8('0') form will wrap such positions to uint8(0) which you would not be able to tell apart from the result for input '0'
On the other hand, if you know for sure that the input consists entirely of a mix of '0' and '1' then the uint8 form is fine.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by