generated code will not preserve the expression.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2023-7-18
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.
采纳的回答
Prathamesh
2023-7-18
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 the ‘DataType’ 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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!