Does Setting Variables as Integers for MATLAB Code Generation requires casting every time?
18 visualizzazioni (ultimi 30 giorni)
Gabriel Roper il 13 Mar 2021
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:
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?
Darshan Ramakant Bhat il 14 Mar 2021
The default value of a number in MATLAB is 'double'. You can verify that with the below code :
>> a = 0
If you want to create values of other type you have to do like below explicitely :
>> b = uint8(0)