view examples/code/myset.c @ 20297:5c42ff6f0eb1 stable

Clean up MEX example code. * myfeval.c: Use mxIsChar rather than deprecated mxIsString. * mypow2.c: Validate that input is a double matrix. * myprop.c: Use space after '!' operator to conform to Octave conventions. * myset.c: Use mexPutVariable instead of missing mxSetName and deprecated mexPutArray. Find existing variable EITHER in global workspace OR in caller workspace. Don't check both. * mystruct.c: Clarify input validation message.
author Rik <rik@octave.org>
date Mon, 15 Jun 2015 10:24:13 -0700
parents c8240a60dd01
children
line wrap: on
line source

#include "mex.h"

void
mexFunction (int nlhs, mxArray* plhs[],
             int nrhs, const mxArray* prhs[])
{
  char *str;
  mxArray *v;
  int found = 0;

  if (nrhs != 2 || ! mxIsChar (prhs[0]))
    mexErrMsgTxt ("Arguments must be a symbol name and a value");

  str = mxArrayToString (prhs[0]);

  // FIXME: If variable does not exist, error is reported which prevents
  //        subsequent mexGetArray function from working.
  v = mexGetArray (str, "global");
  if (v)
    {
      mexPrintf ("%s is a global variable with the following value:\n", str);
      mexCallMATLAB (0, NULL, 1, &v, "disp");
      found = 1;
    }

  if (! found)
    v = mexGetArray (str, "caller");

  if (! found && v)
    {
      mexPrintf ("%s is a caller variable with the following value:\n", str);
      mexCallMATLAB (0, NULL, 1, &v, "disp");
    }

  // WARNING!! Can't do this in MATLAB!  Must copy variable first.
  mexPutVariable ("caller", str, prhs[1]);
}