我很难通过matlab的engine API获得(从而设置)图形对象的属性:我总是在以下代码中获得空指针(在windows 8.1上使用R2015b ):
#include <engine.h>
#include <matrix.h>
#include <mex.h>
#include <mat.h>
int main()
{
Engine *MATLAB;
if (!(MATLAB = engOpen(NULL)))
{
//exit failure etc.
}
engEvalString(MATLAB, "clearvars;close all;x=linspace(-pi,pi);figure;h=plot(x,sin(x),'o-b','LineWidth',2.5);");//OK!! got the plot on a new figure
const mxArray *ph = engGetVariable(MATLAB, "h");//OK!!
const char *cname = mxGetClassName(ph);// OK!!!: got cname = matlab.graphics.chart.primitive.Line
size_t ind = 0;
const char *Prop = "LineWidth";
mxArray *p = mxGetProperty(ph,ind,Prop);//bummer !!! - p is always NULL!!
return 0;
}现在,当使用mex编写等效代码时,所有操作都非常完美,如下所示:
1:我正在运行下一个MATLAB脚本:
mex getMex.cpp;%compile getMex.cpp (with VS 2010 Ultimate), see code below
clearvars;close all;x=linspace(-pi,pi);figure;h=plot(x,sin(x),'o-b','LineWidth',2.5);%OK!! got the plot on a new figure
LineWidth = getMex(h);% OK!! LineWidth = 2.5getMex.cpp源文件:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])
{
//some input/output checks here
mxArray *p = mxGetProperty(prhs[0],0,"LineWidth"); //OK!!! - not NULL
double *p2h=mxGetPr(p);//OK!!! *p2h = 2.5
plhs[0] = p;
}在VS 2010中调试mex代码和引擎代码时,我看到相同的dll都加载了,确切地说是。
我的引擎API代码有什么问题?
我在这里错过了什么?
发布于 2016-01-08 07:38:51
-:,这是我从mathworks支持获得的答案:
在C/C++ MATLAB引擎代码中不能使用MEX函数。客户可以使用MEX从MATLAB脚本调用C、C++或Fortran代码。MATLAB引擎API支持MATLAB与C/C++之间的相反交互。使用MATLAB引擎,客户可以在C或C++代码中利用MATLAB的功能。这两个API之间的区别解释了为什么"mxGetProperty“(一个MEX函数)在您的MEX文件"getMex.cpp”中返回"LineWidth“的正确值,但是在MATLAB实现中返回NULL。 尽管如此,您的用例揭示了MATLAB中当前存在的两个差异:
https://stackoverflow.com/questions/34352964
复制相似问题