Why do conditional statements get optimized out in the code generated by Embedded Coder?

3 views (last 30 days)
I have a MATLAB Function Block in my model. From the body of a sub-function in that MATLAB Function Block, an entire else-branch is missing in the C code. Why would Embedded Coder apply optimization to my if-else statement?
if( (my_var >= uint16(0)) && (my_var <= uint16(100)) ) %code here else %code here - completely missing in C code end

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Jan 2025
Edited: MathWorks Support Team on 31 Jan 2025
This is an expected optimization performed by Embedded Coder. For example, the code could be written such that "my_var" can never be outside the range [0, 100], and so Embedded Coder optimized it out. Another possible reason could be related to the 'Optimize using the specified minimum and maximum values' (UseSpecifiedMinMax) setting. To access the release-specific documentation for this setting, execute the following command in the MATLAB R2019b command window:
>> web(fullfile(docroot, 'rtw/ref/optimize-using-the-specified-minimum-and-maximum-values.html'))
The if-statement does a range check on "my_var", which is why it could be that Embedded Coder is performing the min/max optimization. There are a few different places a user could be setting the min/max values:
1) On the root inport:
2) In the Ports and Data Manager of the MATLAB Function Block:
3) In a Simulink.Signal object mapped to that input signal:
Please follow the below link to search for the required information regarding the current release:

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!