//////////////////////////////////////////////////////////////////////// // // 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); }
/*-------------------------------------------------------------------*/ 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; }
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); }