MATLAB Answers


Difference between MATLAB mex API to MATLAB engine API

Asked by Benny
on 18 Dec 2015
Latest activity Edited by Benny
on 18 Dec 2015
I am having diffculties in getting (and, thus, setting) graphic objects' properties via matlab's engine API: I'm always getting NULL pointer in the following code (Am using R2015b on windows 8.1):
#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 =
size_t ind = 0;
const char *Prop = "LineWidth";
mxArray *p = mxGetProperty(ph,ind,Prop);//bummer !!! - p is always NULL!!
return 0;
Now, when writing the equivalent code with mex API, everything works perfectly as follows:
1: Am running the next MATLAB's script:
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.5
the getMex.cpp source file:
#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;
While debugging both the mex code and the engine code in VS 2010 I saw that the same dlls were loaded exactly.
What is the problem with my engine API code?
What am I missing here?


Sign in to comment.

0 Answers