Int_t    TMatlab::Put(TmxArray *a) {
#ifdef HAVE_MATLAB
  return engPutArray((Engine*)fEngine,(mxArray*)a->Getmx());
#else
  return NULL;
#endif
}
JNIEXPORT void JNICALL Java_JMatLink_engPutArrayNATIVE
   (JNIEnv *env, jobject obj, jint engine, jstring arrayS_JNI, jobjectArray valueDD_JNI)
{

    int i, j;

    const char *arrayS;    
    int   rowCount;
    jobject colPtr;
    int   colCount;
    double *tPtrR;  
    jdouble *arrayElements;

    // Check if engine pointer is within allowed region
    if (( engine < 1 ) || ( engine >= enginePMax ))
    {
        return; // Pointer is out of allowed region
    }


    arrayS    = (*env)->GetStringUTFChars(env, arrayS_JNI, 0);
    rowCount        = (*env)->GetArrayLength(env, valueDD_JNI);
    colPtr        = (*env)->GetObjectArrayElement(env, valueDD_JNI, 0);
    colCount        = (*env)->GetArrayLength(env, colPtr);
    //jboolean *isCopy;

    //double *arrayElements = (*env)->GetDoubleArrayElements(env, colPtr, isCopy);

    if (debugB) printf("engPutArray [][] %s %i %i\n", arrayS, rowCount, colCount);

    T = mxCreateDoubleMatrix(rowCount, colCount, mxREAL);
    mxSetName(T, arrayS);
    //printf("matrix created and name set\n");
    tPtrR = mxGetPr(T);

    for (i=0; i<rowCount; i++) {
    //printf("row %i\n",i);
        colPtr = (*env)->GetObjectArrayElement(env, valueDD_JNI, i);
        //printf("got colPtr %i\n",i);
        arrayElements = (*env)->GetDoubleArrayElements(env, colPtr, 0);
        //printf("got array Elements\n");
        for (j=0; j<colCount; j++) {
            //printf("col %i\n",j);
            tPtrR[i + j*rowCount] = arrayElements[j];
        }
    } // rows

    //printf("elements copied\n");

    engPutArray( engineP[ engine ], T );  /* send array to MATLAB */

    mxDestroyArray(T);

    (*env)->ReleaseStringUTFChars(env, arrayS_JNI, arrayS); // free memory
}
Exemple #3
0
PyObject * mlabraw_put(PyObject *, PyObject *args)
{
  char *lName;
  PyObject *lHandle;
  PyObject *lSource;
  mxArray *lArray = NULL;
  //FIXME should make these objects const
  if (! PyArg_ParseTuple(args, "OsO:put", &lHandle, &lName, &lSource)) return NULL;
  if (! PyCObject_Check(lHandle)) {
    PyErr_SetString(PyExc_TypeError, "Invalid object passed as mlabraw session handle");
    return NULL;
  }
  Py_INCREF(lSource);

  if (PyString_Check(lSource)) {
    lArray = char2mx(lSource);
  } else {
    lArray = numeric2mx(lSource);
  }
  Py_DECREF(lSource);

  if (lArray == NULL) {
    return NULL;   // Above converter already set error message
  }


// for matlab version >= 6.5 (FIXME UNTESTED)
#ifdef _V6_5_OR_LATER
  if (engPutVariable((Engine *)PyCObject_AsVoidPtr(lHandle), lName, lArray) != 0) {
#else
  mxSetName(lArray, lName);
  if (engPutArray((Engine *)PyCObject_AsVoidPtr(lHandle), lArray) != 0) {
#endif
    PyErr_SetString(mlabraw_error,
                   "Unable to put matrix into MATLAB(TM) workspace");
    mxDestroyArray(lArray);
    return NULL;
  }
  mxDestroyArray(lArray);
  Py_INCREF(Py_None);
  return Py_None;
}

static PyMethodDef MlabrawMethods[] = {
  { "open",       mlabraw_open,       METH_VARARGS, open_doc },
  { "close",      mlabraw_close,      METH_VARARGS, close_doc },
  { "oldeval",    mlabraw_oldeval,    METH_VARARGS, ""       },
  { "eval",       mlabraw_eval,       METH_VARARGS, eval_doc },  //FIXME doc
  { "get",        mlabraw_get,        METH_VARARGS, get_doc },
  { "put",        mlabraw_put,        METH_VARARGS, put_doc },
  { NULL,         NULL,               0           , NULL}, // sentinel
};

PyMODINIT_FUNC initmlabraw(void)
{
  PyObject *module =
    Py_InitModule4("mlabraw",
      MlabrawMethods,
"Mlabraw -- Low-level MATLAB(tm) Engine Interface\n"
"\n"
"  open  - Open a MATLAB(tm) engine session\n"
"  close - Close a MATLAB(tm) engine session\n"
"  eval  - Evaluates a string in the MATLAB(tm) session\n"
"  get   - Gets a matrix from the MATLAB(tm) session\n"
"  put   - Places a matrix into the MATLAB(tm) session\n"
"\n"



"The Numeric package must be installed for this module to be used.\n"
"\n"
"Copyright & Disclaimer\n"
"======================\n"
"Copyright (c) 2002-2007 Alexander Schmolck <*****@*****.**>\n"
"\n"
"Copyright (c) 1998,1999 Andrew Sterian. All Rights Reserved. mailto: [email protected]\n"
"\n"
"Copyright (c) 1998,1999 THE REGENTS OF THE UNIVERSITY OF MICHIGAN. ALL RIGHTS RESERVED \n"
"\n"
"Permission to use, copy, modify, and distribute this software and its\n"
"documentation for any purpose and without fee is hereby granted, provided\n"
"that the above copyright notices appear in all copies and that both these\n"
"copyright notices and this permission notice appear in supporting\n"
"documentation, and that the name of The University of Michigan not be used\n"
"in advertising or publicity pertaining to distribution of the software\n"
"without specific, written prior permission.\n"
"\n"
"THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION AS TO ITS FITNESS\n"
"FOR ANY PURPOSE, AND WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR\n"
"IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF\n"
"MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF THE\n"
"UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES, INCLUDING\n"
"SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY\n"
"CLAIM ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN IF\n"
"IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\n"
"\n",
       0,
       PYTHON_API_VERSION);

  /* This macro, defined in arrayobject.h, loads the Numeric API interface */
  import_array();
  PyModule_AddStringConstant(module, "__version__", MLABRAW_VERSION);
  mlabraw_error = PyErr_NewException("mlabraw.error", NULL, NULL);
  Py_INCREF(mlabraw_error);
  PyModule_AddObject(module, "error", mlabraw_error);
}
Exemple #4
0
/*
 * JS Changed coord arguments to double as external function API changed
 */
int matlab_demo_(float *data,
		 double *xcoords, int *xsize,
		 double *ycoords, int *ysize,
		 float *bad_flag) {
  mxArray *T = NULL, *result = NULL;
  mxArray *mX = NULL, *mY = NULL;
  char buffer[BUFSIZE], cbuffer[BUFSIZE];
  double *ddata = 0, *mxData = 0, *myData = 0;;

				/* Open MATLAB engine */
  if (ep == 0){
    if (!(ep = engOpen("\0"))) {
      fprintf(stderr, "\nCan't start MATLAB engine\n");
      return EXIT_FAILURE;
    }
  }

  engOutputBuffer(ep, buffer, BUFSIZE);
				/* Convert from float->double */
  T = mxCreateDoubleMatrix(*xsize, *ysize, mxREAL);
  mxSetName(T, "FERRET_DATA");
  ddata = mxGetPr(T);

  mX = mxCreateDoubleMatrix(*xsize, 1, mxREAL);
  mxSetName(mX, "FERRET_XDATA");
  mxData = mxGetPr(mX);
  mY = mxCreateDoubleMatrix(*ysize, 1, mxREAL);
  mxSetName(mY, "FERRET_YDATA");
  myData = mxGetPr(mY);
  
  {
    int i;
    for (i=0; i < *xsize * *ysize; ++i){
      float theData = data[i];
      if (theData == *bad_flag){
	ddata[i] = NaN;
      } else {
	ddata[i] = data[i];
      }
    }

    memcpy(mxData, xcoords, sizeof(double)* *xsize);
    memcpy(myData, ycoords, sizeof(double)* *ysize);
  }

  /* Place the variables into the MATLAB workspace */
  engPutArray(ep, T);
  engPutArray(ep, mX);
  engPutArray(ep, mY);

  
  sprintf(cbuffer, "ferretdemo(FERRET_DATA', FERRET_XDATA', FERRET_YDATA');");
  engEvalString(ep, cbuffer);
  /*
   * Echo the output from the command.  First two characters are
   * always the double prompt (>>).
   */
  printf("%s", buffer+2);

  mxDestroyArray(T);
/*   engEvalString(ep, "close;"); */
/*   engClose(ep); */
	
  return EXIT_SUCCESS;
}