changeset 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 2691947f5409
children efe22f9b53a3
files examples/code/myfeval.c examples/code/mypow2.c examples/code/myprop.c examples/code/myset.c examples/code/mystruct.c
diffstat 5 files changed, 18 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/examples/code/myfeval.c	Mon Jun 15 09:07:17 2015 -0700
+++ b/examples/code/myfeval.c	Mon Jun 15 10:24:13 2015 -0700
@@ -10,7 +10,7 @@
 
   mexPrintf ("I have %d inputs and %d outputs\n", nrhs, nlhs);
 
-  if (nrhs < 1 || ! mxIsString (prhs[0]))
+  if (nrhs < 1 || ! mxIsChar (prhs[0]))
     mexErrMsgTxt ("ARG1 must be a function name");
 
   str = mxArrayToString (prhs[0]);
--- a/examples/code/mypow2.c	Mon Jun 15 09:07:17 2015 -0700
+++ b/examples/code/mypow2.c	Mon Jun 15 10:24:13 2015 -0700
@@ -8,8 +8,8 @@
   mwIndex i;
   double *vri, *vro;
 
-  if (nrhs != 1 || ! mxIsNumeric (prhs[0]))
-    mexErrMsgTxt ("ARG1 must be a matrix");
+  if (nrhs != 1 || ! mxIsDouble (prhs[0]))
+    mexErrMsgTxt ("ARG1 must be a double matrix");
 
   n = mxGetNumberOfElements (prhs[0]);
   plhs[0] = mxCreateNumericArray (mxGetNumberOfDimensions (prhs[0]),
--- a/examples/code/myprop.c	Mon Jun 15 09:07:17 2015 -0700
+++ b/examples/code/myprop.c	Mon Jun 15 10:24:13 2015 -0700
@@ -9,10 +9,10 @@
 
   if (nrhs < 2 || nrhs > 3)
     mexErrMsgTxt ("incorrect number of arguments");
-  if (!mxIsDouble (prhs[0]))
-    mexErrMsgTxt ("handle expected to be a double scalar");
-  if (!mxIsChar (prhs[1]))
-    mexErrMsgTxt ("expected property to be a string");
+  if (! mxIsDouble (prhs[0]))
+    mexErrMsgTxt ("handle must be a double scalar");
+  if (! mxIsChar (prhs[1]))
+    mexErrMsgTxt ("property must be a string");
 
   handle = mxGetScalar (prhs[0]);
   mxGetString (prhs[1], property, 256);
--- a/examples/code/myset.c	Mon Jun 15 09:07:17 2015 -0700
+++ b/examples/code/myset.c	Mon Jun 15 10:24:13 2015 -0700
@@ -6,29 +6,32 @@
 {
   char *str;
   mxArray *v;
+  int found = 0;
 
-  if (nrhs != 2 || ! mxIsString (prhs[0]))
-    mexErrMsgTxt ("expects symbol name and value");
+  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;
     }
 
-  v = mexGetArray (str, "caller");
+  if (! found)
+    v = mexGetArray (str, "caller");
 
-  if (v)
+  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.
-  mxSetName (prhs[1], str);
-  mexPutArray (prhs[1], "caller");
+  mexPutVariable ("caller", str, prhs[1]);
 }
--- a/examples/code/mystruct.c	Mon Jun 15 09:07:17 2015 -0700
+++ b/examples/code/mystruct.c	Mon Jun 15 10:24:13 2015 -0700
@@ -10,7 +10,7 @@
   const char *keys[] = { "this", "that" };
 
   if (nrhs != 1 || ! mxIsStruct (prhs[0]))
-    mexErrMsgTxt ("expects struct");
+    mexErrMsgTxt ("ARG1 must be a struct");
 
   for (i = 0; i < mxGetNumberOfFields (prhs[0]); i++)
     for (j = 0; j < mxGetNumberOfElements (prhs[0]); j++)