Make matlab use single as standard for code generation

6 次查看(过去 30 天)
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...

请先登录,再进行评论。


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