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

조회 수: 9 (최근 30일)
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?

채택된 답변

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2021년 3월 14일
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
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2021년 3월 14일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 14일
b = uint8(0)
do_something(b)
b(:) = 1
b(:) = do_something_else(b)
  댓글 수: 3
Gabriel Roper
Gabriel Roper 2021년 3월 14일
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.
Walter Roberson
Walter Roberson 2021년 3월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by