具有多个输入的圆形函数在 Simulink 的 MATLAB 函数块中不起作用

Round function with multiple inputs not working in MATLAB function block in Simulink

我想将数字四舍五入到小数点后三位。在 MATLAB 中,我可以通过这种方式轻松完成:

>> number=25.0001;
>> round(25.0001,3)

ans =

25

如果我尝试在 Simulink 中以这种方式在 MATLAB 函数块中执行此操作:

function D_avg1 = fcn(m)
%#codegen

D_avg1 = round(m,3);

它给出错误:

Error calling 'round'. This call-site passes more inputs to this function than it can accept.
Function 'BSD_System /Averaging/MATLAB Function' (#278.45.55), line 4, column 10:
"round(m,3)"
Component: MATLAB Function | Category: Coder error

您可能应该在嵌入式 matlab 函数中包含 eml.extrinsic('round')。我有类似的问题,对于我使用的任何 MATLAB 函数,我将它们包含在 eml.extrinsic('') 中将解决问题,如下所示:

function D_avg1 = fcn(m)
%#codegen

eml.extrinsic('round');

D_avg1 = round(m,3);

据我所知,现在应该可以了。

MATLAB Function 模块仅支持 round 函数的单输入语法。请参阅 http://www.mathworks.com/help/coder/ug/functions-supported-for-code-generation--alphabetical-list.html 处的函数列表文档,其中针对 round 函数您将看到 "Supports only the syntax Y = round(X)."

正如 smyslov 所提到的,如果您需要此语法,则需要将其设为外部语法。但外部仅适用于模拟。对于嵌入式代码生成,外部函数不会生成任何代码。

有答案。只需这样做:

function D_avg1 = fcn(m)
%#codegen

D_avg1 = round((m*1000)/1000);