您能解释一下这个嵌入式 MATLAB 函数错误吗?

发布于 2024-08-01 16:34:17 字数 462 浏览 4 评论 0原文

我在将值从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

絕版丫頭 2024-08-08 16:34:17

我相信 MikeT 是正确的:您无法重新定义变量的大小在嵌入函数中。 如果您查看此 嵌入式 MATLAB 函数文档页面定义局部变量小节下,它说:

一旦定义了变量,就不能
将其重新定义为任何其他类型或大小
在函数体中。

您将必须重新设计您的嵌入式函数,以便您声明的变量不会改变大小。 由于我不知道您随后要使用变量 f 做什么,因此我无法为您提供更多具体帮助。

一般来说,如果您绝对需要使用改变大小的数据,一种解决方案是用“垃圾”值填充数据以保持恒定的大小。 例如:

MAX_ELEMS = 1000;  % Define the maximum number of elements in the vector
f = [fstart:MAX_ELEMS nan(1,fstart-1)];  % Create vector and pad with NaNs

在上面的例子中,变量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:

Once you define a variable, you cannot
redefine it to any other type or size
in the function body.

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:

MAX_ELEMS = 1000;  % Define the maximum number of elements in the vector
f = [fstart:MAX_ELEMS nan(1,fstart-1)];  % Create vector and pad with NaNs

In the above example, the variable f will always have 1000 elements (assuming the value of fstart is an integer value less than or equal to 1000). The value NaN is used to pad the vector to the appropriate constant size. Any subsequent code would have to be able to recognize that a value of NaN should be ignored. Depending on what calculations are subsequently done in the embedded function, different pad values might be needed in place of NaN (such as 0, negative values, etc.).

很糊涂小朋友 2024-08-08 16:34:17

我相信您遇到的问题是您无法在模拟期间更改会导致信号维度发生变化的参数。 代码

f = fstart:f_end;

在您的示例中,只要 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,

f = fstart:f_end;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文