//////////////////////////////////////////////////////////////////////// // // create a MAT file from a double passed in // int createMAT(const char *file, double *data, int datalen) { MATFile *pmat; mxArray *pa1; printf("Creating a .MAT file %s...\n", file); pmat = matOpen(file, "w"); if (pmat == NULL) { printf("Error creating file %s\n", file); printf("(do you have write permission in this directory?)\n"); return(1); } pa1 = mxCreateDoubleMatrix(1,datalen,mxREAL); mxSetName(pa1, "iroro_debug_variable"); memcpy((char *)(mxGetPr(pa1)),(char *)data, 1*datalen*sizeof(double)); matPutArray(pmat, pa1); // clean up mxDestroyArray(pa1); if (matClose(pmat) != 0) { printf("Error closing file %s\n",file); return(1); } printf("Done\n"); return(0); }
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 }
/*-------------------------------------------------------------------*/ void m2pvme_upkarray_rest(int nlhs, mxArray *plhs[], int nrhs, mxArrayIn *prhs[]) { /*-------------------------------------------------------------------*/ MATFile *pmat; char fname[256]; mxArray *array_ptr; plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); if((mxGetPr(plhs[1]))[0] = (double) pvme_upkarray_rest(&plhs[0],(char*)0)==1) { /* the data contains Matlab user defined objects! */ /* save and load this mxArray so as it will be recognized as an object */ /* object passing could be done by getting a default object from the workspace and then constructing the object in the workspace directly by using mxPutArray with each struct for each level... */ /* array_ptr = mexGetArray("def","base"); mxSetName(array_ptr, "defcopy"); mexPutArray(array_ptr, "base"); mxDestroyArray(array_ptr); */ sprintf(fname,"%s/dp_%d.mat",TMP_LOC,pvm_mytid()); /*printf(fname,"%s/dp_%d.mat",TMP_LOC,pvm_mytid());*/ pmat = matOpen(fname, "w"); if (pmat == NULL) { mexErrMsgTxt("m2libpvme:Error creating file for transferring object\n"); } mxSetName(plhs[0], "objsl"); if (matPutArray(pmat, plhs[0]) != 0) { mexErrMsgTxt("m2libpvme:m2pvme_upkarray_rest: Error saving temporary intermediate file for objects\n"); } if (matClose(pmat) != 0) { mexErrMsgTxt("m2libpvme:m2pvme_upkarray_rest:Error temporary intermediate file for object transfer\n"); } /* printf("class::::%s\n", mxGetClassName(plhs[0]));*/ if ( !( plhs[0] = mxCreateString(fname) ) ) mexErrMsgTxt("m2pvme_upkarray_rest(): mxCreateString() failed.\n"); } return; }
static void createVarInWS(char *var_name) { mxArray *var_ptr; var_ptr = mxCreateDoubleMatrix(0,0,mxREAL); mxSetName(var_ptr,var_name); if ( mexPutArray(var_ptr,"caller") ) { mexErrMsgTxt("createPersVarInWS(): mexPutArray() failed.\n"); } return; }
static void restoreVarInWS(int idx) { mxArray *var_ptr; var_ptr = mxCreateDoubleMatrix(PERSVAR_DIMS[idx][0], PERSVAR_DIMS[idx][1], mxREAL); mxSetName(var_ptr,PERSVAR_NAMES[idx]); memcpy(mxGetPr(var_ptr),PERSVAR_DATA[idx], PERSVAR_DIMS[idx][0] * PERSVAR_DIMS[idx][1] * sizeof(double)); if ( mexPutArray(var_ptr,"caller") ) { mexErrMsgTxt("restorePersVarInWS(): mexPutArray() failed.\n"); } return; }
/*-------------------------------------------------------------------*/ void m2pvme_pkarray(int nlhs, mxArray *plhs[], int nrhs, mxArrayIn *prhs[]) { /*-------------------------------------------------------------------*/ char *name; int strl; mxArray *array_ptr; strl = mxGetN(prhs[1])+1; if ( !( name = (char*) mxCalloc(strl,sizeof(char)) ) ) mexErrMsgTxt("m2pvme_pkarray(): mxCalloc() failed.\n"); if ( mxGetString(prhs[1],name,strl)) mexErrMsgTxt("m2pvme_pkarray(): mxGetString() failed.\n"); plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); array_ptr = (mxArray*) prhs[0]; mxSetName(array_ptr, name); (mxGetPr(plhs[0]))[0] = (double) pvme_pkarray(array_ptr); return; }
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); }
/* * 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; }
void main(int argc, char **argv) { char *in_filename, *out_filename; FILE *in_file; MATFile_t *out_file; int rows, cols; mxArray_t *x, *y, *dimensions, *numcategories; printf("Text file to MAT file converter $Revision$\n"); printf("Copyright Jeremy Barnes, 24/8/1999\n"); argv0 = argv[0]; /* Check that we have the right number of arguments... */ if (argc != 3) { fprintf(stderr, "%s: invalid number of arguments (%d)\n", argv[0], argc); fprintf(stderr, "usage: %s <input_file> <output_file>\n", argv[0]); exit(1); } /* Get our filenames */ in_filename = argv[1]; out_filename = argv[2]; printf("Converting %s ==> %s...", in_filename, out_filename); /* Check that we can open our input and output files */ in_file = fopen(in_filename, "r"); if (in_file == NULL) { perror(argv[0]); exit(1); } out_file = matOpen(out_filename, "w"); if (out_file == NULL) { fprintf(stderr, "%s: error opening file %s for output\n", argv[0], out_filename); exit(1); } /* Read through the file once to work out how big we want our matrix to be */ figure_out_size(in_file, &rows, &cols); /* Create our x and y arrays */ x = mxCreateDoubleMatrix(rows, cols-1, mxREAL); if (x == NULL) { fprintf(stderr, "%s: error creating %d by %d matrix x\n", argv[0], rows, cols-1); exit(2); } mxSetName(x, "x"); y = mxCreateDoubleMatrix(rows, 1, mxREAL); if (x == NULL) { fprintf(stderr, "%s: error creating %d by 1 matrix y\n", argv[0], rows); exit(2); } mxSetName(y, "y"); /* Read through it again, this time getting the data out */ read_data(in_file, x, y, rows, cols); /* Record the number of dimensions */ dimensions = mxCreateDoubleMatrix(1, 1, mxREAL); if (dimensions == NULL) { fprintf(stderr, "%s: error creating 1 by 1 matrix dimensions\n", argv[0]); exit(2); } *(mxGetPr(dimensions)) = (double)(cols - 1); mxSetName(dimensions, "dimensions"); /* Record the number of categories */ numcategories = mxCreateDoubleMatrix(1, 1, mxREAL); if (numcategories == NULL) { fprintf(stderr, "%s: error creating 1 by 1 matrix numcategories\n", argv[0]); exit(2); } *(mxGetPr(numcategories)) = 2.0; mxSetName(numcategories, "numcategories"); /* Write our file */ matPutArray(out_file, x); matPutArray(out_file, y); matPutArray(out_file, dimensions); matPutArray(out_file, numcategories); printf(" done.\n"); /* Clean up */ mxDestroyArray(x); mxDestroyArray(y); mxDestroyArray(dimensions); mxDestroyArray(numcategories); fclose(in_file); matClose(out_file); }