我正在 simulink 中研究 S 函数。MATLAB 工作区中有一些变量可用。我想打电话给他们。
所以在 MATLAB 中:
a=3;
在 S 函数(用 C/C++ 编写(中:
double a = CallFromMATLABWorkSpace(a); //Something like this.
我该怎么做?有类似mexCallMATLAB
的东西,但不清楚在这种情况下我应该如何使用它。
若要从工作区获取数据,请使用函数 mexGetVariable。
但是,这是一件有点不寻常的事情。为什么数据不作为参数传递给 S 函数?
从我在mexCallMATLAB
文档中看到的内容以及与C++源代码的互操作来看,它看起来像下面这样:
假设您有一个 MatLab 函数MyDoubleFunction
,该函数采用单个标量双精度值并返回标量双精度值。 如果要向函数传递值 4.0
并查看答案是什么,则需要执行以下操作:
//setup the input args
mxArray* input_args[1] = {mxCreateDoubleScalar(4.0)};
mxArray** output_args; //will be allocated during call to mexCallMATLAB
//make the call to the Matlab function
if (mexCallMATLAB( 1 /* number of output arguments */,
output_args,
1 /* number of input arguments */,
&input_args,
"MyDoubleFunction"))
{
//error if we get to this code block since it returned a non-zero value
}
//inspect the output arguments
double answer = mxGetScalar(*output_args);