Main Content

Upgrade MEX Files to Use Graphics Objects

MATLAB® Version 8.4 (R2014b) changes the data type of handles to graphics objects from double to object.

Before Version 8.4, MEX files used the C/C++ and Fortran API Reference library functions mexGet and mexSet, which declare the input handle argument as type double. If your MEX function uses mexGet or mexSet, MATLAB displays the following error.

Error using mex
Deprecated MEX function mexGet|mexSet was called. Either update the source code
to use mxGetProperty|mxSetProperty, OR rerun MEX with the -DMEX_DOUBLE_HANDLE
added to the command line to enter compatibility mode.

To upgrade your MEX file, consider one or more of the following actions.

Replace mexGet and mexSet Functions

To upgrade a MEX file to use a graphics object, replace calls to mexGet with mxGetProperty and calls to mexSet with mxSetProperty. The following program listings show an example of a before and after source MEX file.

The following code uses mexCallMATLAB to create a plot, which returns the graphics handle in variable plhs[0]. To change the line color, the example uses mxGetScalar to convert the handle to a double, then passes it to mexGet and mexSet.

#include "mex.h"
#define RED   0
#define GREEN 1
#define BLUE 2

void fill_array(double *x)
{
    int i = 0;
    for(i = 0 ; i < 4 ; i++)
    {
        x[i] = i+1;
    }
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
   mxArray *color;
   int ret;
   double handle;
   mxArray *copycolor;
   double *acolor;
   
   mxArray *data = mxCreateDoubleMatrix(1,4,mxREAL);
   fill_array(mxGetPr(data));
   
   ret = mexCallMATLAB(1,&plhs[0],1,&data,"plot");
   if(!ret)
   {
      handle = mxGetScalar(plhs[0]);
      color = mexGet(handle,"Color");
      copycolor = mxDuplicateArray(color);
      acolor = mxGetPr(copycolor);
      acolor[RED] = (1 + acolor[RED]) /2;
      acolor[GREEN] = acolor[GREEN]/2;
      acolor[BLUE] = acolor[BLUE]/2;
      
      mexSet(handle,"Color",copycolor);
      mxSetProperty(plhs[0],0,"Color",copycolor);
   }
}

When you build this MEX file, MATLAB displays an error message.

To change the source file, make the following edits. This code uses the variable plhs[0] in mxGetProperty to get the Color property directly. There is no need to create an intermediate handle variable.

#include "mex.h"
#define RED   0
#define GREEN 1
#define BLUE  2

void fill_array(double *x)
{
    int i = 0;
    for(i = 0 ; i < 4 ; i++)
    {
        x[i] = i+1;
    }
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
   mxArray *color;
   int ret;
   
   mxArray *copycolor;
   double *acolor;
  
   mxArray *data = mxCreateDoubleMatrix(1,4,mxREAL);
   fill_array(mxGetPr(data));
   
   ret = mexCallMATLAB(1,&plhs[0],1,&data,"plot");
   if(!ret)
   {
      color = mxGetProperty(plhs[0],0,"Color");
      copycolor = mxDuplicateArray(color);
      acolor = mxGetPr(copycolor);
      acolor[RED] = (1 + acolor[RED]) /2;
      acolor[GREEN] = acolor[GREEN]/2; 
      acolor[BLUE] = acolor[BLUE]/2;
      
      mxSetProperty(plhs[0],0,"Color",copycolor); 
   }
}

To build this MEX file, type:

mex mymex.c
Building with 'Microsoft Visual C++ 2012 (C)'.
MEX completed successfully.

Alternatively, you can build the original source file by following the steps in I Want to Rebuild MEX Source Code Files.

mex Automatically Converts Handle Type

If your MEX function uses the mexCallMATLAB or mexGetVariable functions to get a graphics handle and to pass the handle to the mexGet and mexSet APIs, then MATLAB automatically detects that behavior and your MEX function continues to execute correctly. You know that your MEX function uses this pattern if the function executes without error.

If you rebuild this MEX file in MATLAB R2014b or later, MATLAB displays an error message. To rebuild the file, follow the instructions in either Replace mexGet and mexSet Functions or I Want to Rebuild MEX Source Code Files.

I Want to Rebuild MEX Source Code Files

If you rebuild your MEX source files in MATLAB R2014b or later, MATLAB displays an error message.

You might be able to use the mex command compatibility flag, -DMEX_DOUBLE_HANDLE, to build the MEX file to work with graphics objects. If the MEX function calls a function that returns a graphics handle using the mexCallMATLAB or mexGetVariable functions, MATLAB automatically detects and converts the handle type. To build the source file, mymex.c, type:

mex -DMEX_DOUBLE_HANDLE mymex.c

If you pass a graphics handle to a MEX function, convert the handle to double before calling the function. For more information, see I Do Not Have MEX Source Code File.

I Do Not Have MEX Source Code File

If you get a run-time error and you do not have the source code, you might be able to use the following workaround. Use this workaround only for MEX functions that take a graphics handle as an input argument.

Before you pass a graphics handle to the MEX function, first convert the handle to a double. For example, if you call MEX function mymex:

Y = 1:10;
h = plot(Y); 
mymex(h)

then add a statement to convert the handle h to double:

Y = 1:10;
h = plot(Y);
h = double(h);
mymex(h)

See Also

|

Related Topics