您能解释一下这个嵌入式 MATLAB 函数错误吗?
我在将值从 GUI 发送到 Simulink 模型中的嵌入式 MATLAB 函数 (EMF) 时遇到问题。 我从 GUI 中的滑块获取该值,并将其发送到模型中的 EMF 块。 我可以确认该值已从 GUI 正确传输到 Simulink 模块,因为我可以在模型中使用显示模块显示该值,并在更改 GUI 中的滑块位置时查看值的变化。 但是,当我运行模型时,我不断收到此错误:
Could not determine the size of this expression.
Function 'Kastl' (#18.282.283), line 14, column 1:
"f"
这是我的 EMF 块代码的一部分:
function y = input_par(u,fstart)
...
f_end = 1000;
f = fstart:f_end;
...
I'm having a problem sending a value from a GUI to an Embedded MATLAB Function (EMF) in a Simulink model. I get this value from a slider in my GUI and send it to an EMF block in my model. I can confirm that the value is being transferred correctly from my GUI to my Simulink block, since I can display the value with a display block in my model and see the value change when I change the slider position in my GUI. However I keep getting this error when I run my model:
Could not determine the size of this expression.
Function 'Kastl' (#18.282.283), line 14, column 1:
"f"
This is part of my EMF block code:
function y = input_par(u,fstart)
...
f_end = 1000;
f = fstart:f_end;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信 MikeT 是正确的:您无法重新定义变量的大小在嵌入函数中。 如果您查看此 嵌入式 MATLAB 函数文档页面在定义局部变量小节下,它说:
您将必须重新设计您的嵌入式函数,以便您声明的变量不会改变大小。 由于我不知道您随后要使用变量
f
做什么,因此我无法为您提供更多具体帮助。一般来说,如果您绝对需要使用改变大小的数据,一种解决方案是用“垃圾”值填充数据以保持恒定的大小。 例如:
在上面的例子中,变量
f
将始终有1000个元素(假设fstart
的值是小于或等于1000的整数值)。 值NaN
用于将向量填充到适当的常量大小。 任何后续代码都必须能够识别应忽略NaN
值。 根据嵌入函数中随后进行的计算,可能需要不同的填充值来代替 NaN(例如 0、负值等)。I believe MikeT is correct: you can't redefine the size of a variable in an embedded function. If you look at this Embedded MATLAB Function documentation page under the subsection Defining Local Variables, it says:
You will have to rework your embedded function such that the variables you declare are not changing size. Since I don't know what you are subsequently doing with the variable
f
, there's not much more specific help I can give you.In general, if you absolutely need to use data that changes size, one solution is to pad the data with "garbage" values in order to maintain a constant size. For example:
In the above example, the variable
f
will always have 1000 elements (assuming the value offstart
is an integer value less than or equal to 1000). The valueNaN
is used to pad the vector to the appropriate constant size. Any subsequent code would have to be able to recognize that a value ofNaN
should be ignored. Depending on what calculations are subsequently done in the embedded function, different pad values might be needed in place ofNaN
(such as 0, negative values, etc.).我相信您遇到的问题是您无法在模拟期间更改会导致信号维度发生变化的参数。 代码
在您的示例中,只要 fstart 发生变化, 就会更改大小。 我认为这就是 EMF 块所抱怨的。 对于这个特定问题,我没有任何简单的解决方法,但也许有一种等效的方法可以避免这个问题。
I believe the issue you are running into is that you can't change a parameter during simulation that will cause the dimension of a signal to change. In your example, the code,
changes size whenever fstart changes. I think this is what EMF block is complaining about. I don't have any easy workaround for this particular issue, but maybe there's an equivalent way of doing what you want that avoids this issue.