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

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?

 Réponse acceptée

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 commentaires

@Darshan Ramakant Bhat I understand that I have to do that when I first set up the variabe. But my question is, do I have to do that every time I set a new value to the variable? For example, in the following script:
b=uint8(0)
do_something(b)
b = 1
b = do_something_else(b)
Do I have to use uint8() three times, or just the first time specifically when using MATLAB embedded coder to convert this to C/C++?
Or you index the output, like I show in my answer. Indexed outputs automatically do type conversion.
Thanks @Walter Roberson. Yes indexed outputs are better way to do it.
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

Connectez-vous pour commenter.

Plus de réponses (1)

b = uint8(0)
do_something(b)
b(:) = 1
b(:) = do_something_else(b)

3 commentaires

Note: this can potentially generate code that creates a double precision 1 and type-converts it to uint8 for the assignment. Compiler optimizers would be permitted to reduce such code down to a single operation, but that would be a Quality of Implementation.
Wow, having to do it that way is a little bit horrifying, but it does solve the problem.
I wish I could mark both yours and Darshan's answers as correct, but MATLAB Answers only allows one to be accepted.
I marked Darshan's correct as he technically answered the question, but kudos to you for providing a workaroud.
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.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by