MATLAB Answers

Does Setting Variables as Integers for MATLAB Code Generation requires casting every time?

2 views (last 30 days)
I have a MATLAB project (not Simulink) that I am generating C code for to run on a NVIDIA Jetson Nano. In it, I have a persistent variable that will only be a small integer, but MATLAB coder by default sets it to a double.
To work around this, I explicitly cast the variable to be a be a uint8 using the following snippet:
if isempty(example_var)
example_var=uint8(0);
end
When I then use the Check for Run-Time Issues step in the MATLAB Coder, it fails with the following error
This assignment writes a 'double' value into a 'uint8' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches.
I scrolled down to the point where this error was reported, and found that the line in question in the following:
example_var = 0;
This is (obviously) a number that can be represented with a uint8, but MATLAB Coder seems to have decided to use it as a double. If I cast the variable again here, the error goes away. However, my project sets the variable multiple times, and there are many other variables that I would like to represent as an integer. Do I have to manually cast the variable every time that I set it, or is there a way for matlab to automatically convert the number to a uint8?

Accepted Answer

Darshan Ramakant Bhat
Darshan Ramakant Bhat on 14 Mar 2021
The default value of a number in MATLAB is 'double'. You can verify that with the below code :
>> a = 0
a =
0
>> class(a)
ans =
'double'
If you want to create values of other type you have to do like below explicitely :
>> b = uint8(0)
b =
uint8
0
  5 Comments
Darshan Ramakant Bhat
Darshan Ramakant Bhat on 14 Mar 2021
Casting explicitely to uint8() will aslo work. But like Walter suggested, indexing will keep the type
>> b = 3 % b will be of type double
>> b = uint8(3) % b will be of type uint8, old type is over-written
>> b(:) = 5 % b is still uint8, but the new value is uint8(5). Type is not lost
>> b(:) = 674 % Observe the output, it is saturated at uint8 max value of 255
>> b = uint16(4) % Now the type of b will be uint16

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 14 Mar 2021
b = uint8(0)
do_something(b)
b(:) = 1
b(:) = do_something_else(b)
  3 Comments
Walter Roberson
Walter Roberson on 14 Mar 2021
uint8(0) and other numeric type name conversions with a literal constant are handled at parse time -- for example uint64(20000000000000000001) is not computed as double precision first and then converted. Effectively they become like keywords.
type names with an expression that is not a literal constant are handled at run-time even if they could be handled at compile time. I think... it can be hard to tell.
So when assigning a constant that is not double precision, it is safest and potentially more efficient to put the type name with it instead of relying on indexing to do type conversion.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by