generated code will not preserve the expression.

2 Ansichten (letzte 30 Tage)
Jaya Mane
Jaya Mane am 23 Jun. 2023
Kommentiert: Jaya Mane am 6 Dez. 2023
I want to save new variable as "Upper Limit " in sldd, which is calculated using the other variables. I don't want to do the mathematical opearation of multiplication instead I am trying to assign the value for upper limit variable as as
"(GRID_NORM_FREQ +(GRID_NORM_FREQ*5/100))*2*PI' "
So I can simulate, but while generating code for the same, I am facing this error.
Parameter object 'Value' property uses an expression '(GRID_NORM_FREQ +
(GRID_NORM_FREQ*5/100))*2*PI' involving division operator. Under this condition, generated code
will not preserve the expression.
  1 Kommentar
Walter Roberson
Walter Roberson am 18 Jul. 2023
I suspect that the division is being replaced by multiplication by a reciprical -- that GRID_NORM_FREQ*5/100 is going to have code generated as if it were GRID_NORM_FREQ*5*0.01 . That might not generate exactly the same results
Also remember that when you code something like A * 2 * Pi then each element of A would be multiplied by 2 and the result would be multiplied by Pi for a total of numel(A) times 2 multiplications -- MATLAB is not certain to optimize it to A*(2*Pi) which would be numel(A)+1 multiplications.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Prathamesh
Prathamesh am 18 Jul. 2023
Hi,
I understand that you are encountering the error message ‘generated code will not preserve the expression’
For the generated code to preserve the expression other than auto theDataType property of the object that uses the expression should be set to the same value. Otherwise, the code generator does not preserve the expression.
Here the expression "(GRID_NORM_FREQ +(GRID_NORM_FREQ*5/100)) *2*PI' " is not producing the value with same ‘DataType’ due to presence of division operator.
You can try to typecast the whole expression in same DataType’.
For example, if the output is expected to be a ‘single’ value then typecast the expression in single,
single((GRID_NORM_FREQ +(GRID_NORM_FREQ*5/100)) *2*PI')
For more information you can refer to the documentation attached below.
I hope this resolves you issue.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink Coder finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by