Make matlab use single as standard for code generation

조회 수: 7 (최근 30일)
Rasmus
Rasmus 2021년 10월 16일
댓글: Rasmus 2021년 10월 26일
How do I tell matlab to use single as standard data type when using embedded coder for simulink, for example in an embedded matlab function?
Right now if i write:
test_struct.var1 = 1;
it uses a double. I could write everywhere:
test_struct.var1 = single(1);
however, this completely clutters up the code. Is there a way to tell the coder to always use singles instead of doubles?
Kind regards,
Rasmus,

답변 (2개)

Jonas
Jonas 2021년 10월 18일
In the 'Configuration Parameters', under the section 'Math and Data Types', choose 'single' for the option 'Default for underspecified data type' instead of 'double'.
  댓글 수: 4
Jonas
Jonas 2021년 10월 18일
Yes, and I agree. I believe the same is also true for Stateflow charts, as a Stateflow is in fact an Embedded Function behind the scenes.
Backward propagation rules do not seem to apply to Stateflows or MATLAB Functions.
When I have a function inside a Stateflow which only accepts uint32 as an input, providing a constant value '500' will error, since it interprets the '500' as a double and it doesn't interpret it as uint32 as a backward propagation...
Rasmus
Rasmus 2021년 10월 18일
Good to know. Thank you for the answers!

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


Andy Bartlett
Andy Bartlett 2021년 10월 18일
Embedded Coder is designed to keep the generated code consistent with a Simulink model's simulation behavior. Changing doubles to singles only in the generated code is inconsistent with that goal.
To generate code without doubles, it is recommended to change the data types used in the model from double to single.
Single Precision Converter Tool for Simulink
The Single Precision Converter tool automates the conversion process in many causes as shown in this example. The setting that Jonas mentioned is one of several things that are changed by the tool. While running this tool, a Fixed-Point Designer license is required.
Convert from Double MATLAB Code to Single MATLAB Code
If you want to convert a MATLAB file from double to single, you can use this tool. While running this tool Fixed-Point Designer and MATLAB Coder licenses are required.
Authoring Polymorphic Code
If you'd like to author polymorphic code that can be quickly changed from double to single with a simple configuration change consider the types-table and cast-like pattern. This authoring pattern is described in this technical article Best Practices for Converting MATLAB Code. This approach works for singles too, even though the title specifies Fixed Point. If you search that article for 'single' with the quotes, you'll quickly see a types type that includes singles as one of the options.
In addition to using an external types table, there are few key techniques in the approach.
  1. Use the cast like syntax to control types
  2. When writing to a variable that has already been typed, preserve the type using indexed assignment. To re-aassign the whole variable but preserve type use colon assignment a(:) = b;
As an author becomes familiar with this pattern, the amount of extra code required for data typing can be minimized.
A benefit of this approach, as opposed to one that only changes things in code generation, is that testing and debugging of the code's behavior in MATLAB, Simulink, and generated code will all be consistent.
Cast-like and indexed assignment are available in base MATLAB.
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 10월 18일
I only recently noticed that using
zeros(Size, class(Variable))
can invoke a different method than
zeros(Size, 'like', Variable)
but I have not figured out yet what the semantic difference between the two is intended to be?
Rasmus
Rasmus 2021년 10월 26일
Thank you for the answer.
>>Embedded Coder is designed to keep the generated code consistent with a Simulink model's simulation behavior. Changing doubles to singles only in the generated code is inconsistent with that goal.
But is there a way to make also the simulink model by default use singles in embedded functions?
I also tried to use the Single Precision Converter tool, however, matlab crashes when I try to use it on my matlab embedded function, I will have to look more into this.

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

카테고리

Help CenterFile Exchange에서 Deployment, Integration, and Supported Hardware에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by