可以解释这个嵌入式MATLAB函数的错误吗? [英] Can you explain this Embedded MATLAB Function error?

查看:265
本文介绍了可以解释这个嵌入式MATLAB函数的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Simulink模型中,我将GUI中的值发送到嵌入式MATLAB函数(EMF)时遇到问题。我从GUI中的滑块获取此值,并将其发送到我的模型中的EMF块。我可以确认该值正在从我的GUI正确传输到我的Simulink块,因为我可以在我的模型中显示一个显示块的值,并且在我更改GUI中的滑块位置时看到值的变化。但是,当我运行我的模型时,我不断收到此错误:

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"

这是我的EMF块代码的一部分:

This is part of my EMF block code:

function y = input_par(u,fstart)
  ...
  f_end = 1000;
  f = fstart:f_end; 
  ...


推荐答案

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

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.

您将不得不重做您的嵌入式功能使您声明的变量不会改变大小。由于我不知道你以后用变量 f 做什么,所以没有更多的具体的帮助我可以给你。

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

在上面的例子中,变量 f 将始终具有1000个元素(假设值 fstart 是小于或等于1000的整数值)。值 NaN 用于将向量填充到适当的常量大小。任何后续的代码都必须能够识别出值$ NaN 的值应被忽略。根据随后在嵌入式功能中进行的计算,可能需要不同的pad值代替 NaN (例如0,负值等)。

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.).

这篇关于可以解释这个嵌入式MATLAB函数的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆