Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between MATLAB mex API to MATLAB engine API

Tags:

c++

matlab

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 = 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;
}

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?

like image 817
Benny K Avatar asked Apr 26 '26 15:04

Benny K


1 Answers

Well, apperantly it's impossible )-: , this is the answer I got from mathworks support:

You cannot use MEX API functions in C/C++ MATLAB Engine code. Customers can use the MEX API to call C, C++, or Fortran code from a MATLAB script. The MATLAB Engine API enables the opposite interaction between MATLAB and C/C++. Using the MATLAB Engine, customers can leverage the functionality of MATLAB in C or C++ code. The distinction between the two APIs explains why "mxGetProperty", a MEX API function, returns the correct value for "LineWidth" in your MEX file "getMex.cpp", but returns NULL in the MATLAB Engine implementation.

Nevertheless, your use case brought to light two current discrepancies in MATLAB:

  1. Our documentation does not state that the MEX API should not be used in the context of the MATLAB Engine. We also do not display any error messages to the user if MEX functions are used in MATLAB Engine code. I have submitted this feedback to our development organization in an enhancement request.

  2. You were able to use "mxGetClassName", a MEX function, in your MATLAB Engine code. Being able to do so contradicts the fact that MEX functions should not work in the MATLAB Engine. I have let development know that "mxGetClassName" can be used with the MATLAB Engine.

like image 91
Benny K Avatar answered Apr 28 '26 05:04

Benny K