Esempio n. 1
0
/* ---------------------------------------------------------------------- */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
	int     i, ix, nRows, nCols, nBands, ngot, sz = 1, dims[3];
	size_t  nPts;
	double *pd;
	mxClassID classid = mxUINT8_CLASS;
	FILE *f = NULL;
	mxArray *rslt = NULL;

	if (nrhs < 1) {
		mexPrintf("popenr  Y = popenr(X[,N[,F]])  Open and read an external process\n");
		mexPrintf("        When X is a string, that string is executed as a new process\n");
		mexPrintf("        and Y is returned as a handle (integer) to that stream.\n");
		mexPrintf("        Subsequent calls to popenr(Y,N) with that handle return the next N\n");
		mexPrintf("        values read from the standard ouptut of converted to Matlab values\n");
		mexPrintf("        according to the format string F (default: char).\n");
		mexPrintf("        If N is a row vector with [nRows nCols nBands] and type 'char' then data is\n");
		mexPrintf("        internaly transposed to Matlab orientation and output is a [MxNxnBands] array.\n");
		mexPrintf("        A call with N set to -1 means to close the stream.\n");
		return;
	}
	/* look at the data */
	/* Check to be sure input argument is a string. */
	if ((mxIsChar(prhs[0]))) {
		/* first argument is string - opening a new command */
		char *cmd;
		int tabix = findfreetab();
		
		if (tabix < 0) mexErrMsgTxt("Out of file table slots.");

		cmd = mxArrayToString(prhs[0]);
		f = popen(cmd, "r");
		mxFree(cmd);
		if (f == NULL) mexErrMsgTxt("Error running external command.");

		/* else have a new command path - save the handle */
		filetab[tabix] = f;
		/* return the index */
		if (nlhs > 0) {
			mxArray *rslt = mxCreateDoubleMatrix(1,1, mxREAL);
			plhs[0] = rslt;
			pd = mxGetPr(rslt);
			*pd = (double)tabix;
		}
		return;
	}

	if (nrhs < 2) mexErrMsgTxt("apparently accessing handle, but no N argument");
	
	/* get the handle */
	if (mxGetN(prhs[0]) == 0) mexErrMsgTxt("handle argument is empty");

	ix = (int)*mxGetPr(prhs[0]);
	if (ix < filetabix) f = filetab[ix];
	if (f == NULL) mexErrMsgTxt("invalid handle");

	/* how many items required? */
	if (mxGetN(prhs[1]) == 0) mexErrMsgTxt("length argument is empty");

	nBands = 1;
	pd = mxGetPr(prhs[1]);
	if (mxGetN(prhs[1]) == 1) {
		nRows = (int)pd[0];		nCols = 1;
	}
	else if (mxGetN(prhs[1]) == 2){
		nRows = (int)pd[0];		nCols = (int)pd[1];
	}
	else if (mxGetN(prhs[1]) == 3){
		nRows = (int)pd[0];		nCols = (int)pd[1];		nBands = (int)pd[2];
	}
	else
		mexErrMsgTxt("More than 3 dimensional data is not supported");
	
	/* maybe close */
	if (nRows < 0) {
		pclose(f);
		filetab[ix] = NULL;
		return;
	}

	/* what is the format? */
	if (nrhs > 2) {
		char *fmtstr = NULL;

		if (!mxIsChar(prhs[2])) mexErrMsgTxt("format arg must be a string");

		fmtstr = mxArrayToString(prhs[2]);
		if (!strcmp(fmtstr, "uint8") || !strcmp(fmtstr, "char")) {
			classid = mxUINT8_CLASS;	sz = 1;
		}
		else if (!strcmp(fmtstr, "int8")) {
			classid = mxINT8_CLASS;		sz = 1;
		}
		else if (!strcmp(fmtstr, "int16")) {
			classid = mxINT16_CLASS;	sz = 2;
		}
		else if (!strcmp(fmtstr, "uint16")) {
			classid = mxUINT16_CLASS;	sz = 2;
		}
		else if (!strcmp(fmtstr, "int32")) {
			classid = mxINT32_CLASS;	sz = 4;
		}
		else if (!strcmp(fmtstr, "uint32")) {
			classid = mxUINT32_CLASS;	sz = 4;
		}
		else if (!strcmp(fmtstr, "float") || !strcmp(fmtstr, "single")) {
			classid = mxSINGLE_CLASS;	sz = 4;
		}
		else if (!strcmp(fmtstr, "double")) {
			classid = mxDOUBLE_CLASS;	sz = 8;
		}
		else
			mexErrMsgTxt("unrecognized format");
	}

	/* do the read */
	dims[0] = nRows;		dims[1] = nCols;		dims[2] = nBands;
	rslt = mxCreateNumericArray((nBands == 1 ? 2 : 3), dims, classid, mxREAL);
	nPts = (size_t)nRows * nCols * nBands;
	if (nCols == 1)
		ngot = fread(mxGetData(rslt), sz, nPts, f);
	else {
		int k, row, col, band;
		size_t nXY = (size_t)nRows * nCols;
		void *pv = mxGetData(rslt);
		if (classid == mxUINT8_CLASS) {
			unsigned char *tmp = mxMalloc(nCols * nBands * sz);
			unsigned char *p = (unsigned char *)pv;
			for (row = 0; row < nRows; row++) {
				fread(tmp, sz, nCols * nBands, f);		/* Read a row of nCols by nBands bytes of data */
				for (col = 0; col < nCols; col++) {
					for (band = 0; band < nBands; band++) {
						p[row + col*nRows + band*nXY] = tmp[band + col*nBands];
					}
				}
			}
			mxFree(tmp);
		}
	}


	if (ngot < nPts) {
		void *pv = mxGetData(rslt);
		mxRealloc(pv, ngot * sz);
	}

	plhs[0] = rslt;
}
Esempio n. 2
0
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    int 	i, err, len;
    long   	pvl, pvb[16];

    if (nrhs < 1){
	mexPrintf("popenw     Y=popenw(X[,D[,F]])  Open and write an external process\n");
	mexPrintf("           When X is a string, that string is executed as a new process\n");
	mexPrintf("           and Y is returned as a handle (integer) to that stream.\n");
	mexPrintf("           Subsequent calls to popen(Y,D[,F]) with that handle write\n");
	mexPrintf("           the data in D to the the process, converted to binary\n");
	mexPrintf("           according to the format string F (default: big endian int16).\n");
	mexPrintf("           A call with D empty means to close the stream.\n");
	return;
    }
    /* look at the data */
    /* Check to be sure input argument is a string. */
    if ((mxIsChar(prhs[0]))){
	/* first argument is string - opening a new command */
	FILE *f;
	char *cmd;
	int tabix = findfreetab();
	
	if (tabix < 0) {
	    mexErrMsgTxt("Out of file table slots.");
	} else {
	    cmd = mxArrayToString(prhs[0]);
	    /* fprintf(stderr, "cmd=%s\n", cmd); */
	    f = popen(cmd, "w");
	    mxFree(cmd);
	    if ( f == NULL ) {
		mexErrMsgTxt("Error running external command.");
		return;
	    }
	    /* else have a new command path - save the handle */
	    filetab[tabix] = f;
	    /* return the index */
	    if (nlhs > 0) {
		double *pd;
		mxArray *rslt = mxCreateDoubleMatrix(1,1, mxREAL);
		plhs[0] = rslt;
		pd = mxGetPr(rslt);
		*pd = (double)tabix;
	    }
	}
	return;
    }	

    if (nrhs < 2) {
	mexErrMsgTxt("apparently accessing handle, but no D argument");
	return;
    }
    
    /* get the handle */
    {
	int ix, rows, cols, npts, ngot; 
	int fmt = MXPO_INT16BE;
	int sz = 2;
	FILE *f = NULL;
	double *pd;
	char *data;

	if (mxGetN(prhs[0]) == 0) {
	    mexErrMsgTxt("handle argument is empty");
	    return;
	}

	pd = mxGetPr(prhs[0]);
	ix = (int)*pd;
	if (ix < filetabix) {
	    f = filetab[ix];
	}
	if (f == NULL) {
	    mexErrMsgTxt("invalid handle");
	    return;
	}

	/* how many values to write */
	npts = mxGetN(prhs[1]) * mxGetM(prhs[1]); 
	if (npts == 0) {
	    /* close */
	    pclose(f);
	    filetab[ix] = NULL;
	    return;
	}

	/* what is the format? */
	if ( nrhs > 2 ) {
	    char *fmtstr;

	    if (!mxIsChar(prhs[2])) {
		mexErrMsgTxt("format arg must be a string");
		return;
	    }
	    fmtstr = mxArrayToString(prhs[2]);
	    if (strcmp(fmtstr, "int16n")==0 || strcmp(fmtstr, "int16") == 0) {
		fmt = MXPO_INT16N;
	    } else if (strcmp(fmtstr, "int16r")==0) {
		fmt = MXPO_INT16R;
	    } else if (strcmp(fmtstr, "int16be")==0) {
#if BYTE_ORDER == BIG_ENDIAN
		fmt = MXPO_INT16N;
#else
		fmt = MXPO_INT16R;
#endif
	    } else if (strcmp(fmtstr, "int16le")==0) {
#if BYTE_ORDER == BIG_ENDIAN
		fmt = MXPO_INT16R;
#else
		fmt = MXPO_INT16N;
#endif
	    } else if (strcmp(fmtstr, "uint8")==0) {
		fmt = MXPO_UINT8;
		sz = 1;
	    } else if (strcmp(fmtstr, "char")==0) {
		fmt = MXPO_CHAR;
		sz = 1;
	    } else {
		mexErrMsgTxt("unrecognized format");
	    }
            mxFree(fmtstr);
	}

	data = (char *)malloc(sz*npts+1);

	/* format conversion */
	if (sz == 1) {
	    if (mxIsChar(prhs[1])) {
                if (mxGetString(prhs[1], data, npts+1) != 0) {
                    mxErrMsgTxt("Problems copying string data");
                }
            } else {
       	        int i;
       	        double *pd = mxGetPr(prhs[1]);
       	        char *pc = (char *)data;
       	        pd = pd + npts - 1;
       	        pc = pc + npts - 1;
       	        for(i = npts-1; i >= 0; --i) {
       	  	    *pc-- = (char)*pd--;
                }
            }
	} else if (fmt == MXPO_INT16N) {
	    int i;
	    double *pd = mxGetPr(prhs[1]);
	    short *ps = (short *)data;
	    pd = pd + npts - 1;
	    ps = ps + npts - 1;
	    for(i = npts-1; i >= 0; --i) {
		*ps-- = (short)*pd--;
	    }
	} else if (fmt == MXPO_INT16R) {
	    int i;
	    double *pd = mxGetPr(prhs[1]);
	    short *ps = (short *)data;
	    short v;
	    pd = pd + npts - 1;
	    ps = ps + npts - 1;
	    for(i = npts-1; i >= 0; --i) {
		v = (short)*pd--;
		*ps -- = (0xFF & (v >> 8)) + (0xFF00 & (v << 8));
	    }
	} else {
Esempio n. 3
0
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    int 	i, err, len;
    long   	pvl, pvb[16];


    if (nrhs < 1){
	mexPrintf("popenr     Y=popenr(X[,N[,F]])  Open and read an external process\n");
	mexPrintf("           When X is a string, that string is executed as a new process\n");
	mexPrintf("           and Y is returned as a handle (integer) to that stream.\n");
	mexPrintf("           Subsequent calls to popenr(Y,N) with that handle return\n");
	mexPrintf("           the next N values read from the standard ouptut of\n");
	mexPrintf("           converted to Matlab values according to the format\n");
	mexPrintf("           string F (default: char).  A call with N set to -1\n");
	mexPrintf("           means to close the stream.\n");
	return;
    }
    /* look at the data */
    /* Check to be sure input argument is a string. */
    if ((mxIsChar(prhs[0]))){
	/* first argument is string - opening a new command */
	FILE *f;
	char *cmd;
	int tabix = findfreetab();
	
	if (tabix < 0) {
	    mexErrMsgTxt("Out of file table slots.");
	} else {
	    cmd = mxArrayToString(prhs[0]);
	    /* fprintf(stderr, "cmd=%s\n", cmd); */
	    f = popen(cmd, "r");
	    mxFree(cmd);
	    if ( f == NULL ) {
		mexErrMsgTxt("Error running external command.");
		return;
	    }
	    /* else have a new command path - save the handle */
	    filetab[tabix] = f;
	    /* return the index */
	    if (nlhs > 0) {
		double *pd;
		mxArray *rslt = mxCreateDoubleMatrix(1,1, mxREAL);
		plhs[0] = rslt;
		pd = mxGetPr(rslt);
		*pd = (double)tabix;
	    }
	}
	return;
    }	

    if (nrhs < 2) {
	mexErrMsgTxt("apparently accessing handle, but no N argument");
	return;
    }
    
    /* get the handle */
    {
	int ix, rows, cols, npts, ngot; 
	int fmt = MXPO_INT16N;
	int sz = 2;
	FILE *f = NULL;
	double *pd;
	mxArray *rslt;

	if (mxGetN(prhs[0]) == 0) {
	    mexErrMsgTxt("handle argument is empty");
	    return;
	}

	pd = mxGetPr(prhs[0]);
	ix = (int)*pd;
	if (ix < filetabix) {
	    f = filetab[ix];
	}
	if (f == NULL) {
	    mexErrMsgTxt("invalid handle");
	    return;
	}
	/* how many items required? */
	if (mxGetN(prhs[1]) == 0) {
	    mexErrMsgTxt("length argument is empty");
	    return;
	} else if (mxGetN(prhs[1]) == 1) {
	    rows = (int)*mxGetPr(prhs[1]);
	    cols = 1;
	} else {
	    double *pd = mxGetPr(prhs[1]);
	    rows = (int)pd[0];
	    cols = (int)pd[1];
	}
	
	/* maybe close */
	if (rows < 0) {
	    pclose(f);
	    filetab[ix] = NULL;
	    return;
	}

	/* what is the format? */
	if ( nrhs > 2 ) {
	    char *fmtstr;

	    if (!mxIsChar(prhs[2])) {
		mexErrMsgTxt("format arg must be a string");
		return;
	    }
	    fmtstr = mxArrayToString(prhs[2]);
	    if (strcmp(fmtstr, "int16n")==0 || strcmp(fmtstr, "int16") == 0) {
		fmt = MXPO_INT16N;
	    } else if (strcmp(fmtstr, "int16r")==0) {
		fmt = MXPO_INT16R;
	    } else if (strcmp(fmtstr, "int16be")==0) {
#if BYTE_ORDER == BIG_ENDIAN
		fmt = MXPO_INT16N;
#else
		fmt = MXPO_INT16R;
#endif
	    } else if (strcmp(fmtstr, "int16le")==0) {
#if BYTE_ORDER == BIG_ENDIAN
		fmt = MXPO_INT16R;
#else
		fmt = MXPO_INT16N;
#endif
	    } else if (strcmp(fmtstr, "float")==0) {
		fmt = MXPO_FLOAT;
		sz = 4;
	    } else if (strcmp(fmtstr, "double")==0) {
		fmt = MXPO_DOUBLE;
		sz = 8;
	    } else if (strcmp(fmtstr, "uint8")==0) {
		fmt = MXPO_UINT8;
		sz = 1;
	    } else if (strcmp(fmtstr, "char")==0) {
		fmt = MXPO_CHAR;
		sz = 1;
	    } else {
		mexErrMsgTxt("unrecognized format");
	    }
	}

	/* do the read */
	rslt = mxCreateDoubleMatrix(rows, cols, mxREAL);
	npts = rows*cols;
	ngot = fread(mxGetPr(rslt), sz, npts, f);
	
	/* format conversion */
	if (sz == 4) {
	    int i;
	    double *pd = mxGetPr(rslt);
	    float *pf = (float *)pd;
	    pd = pd + npts - 1;
	    pf = pf + npts - 1;
	    for(i = npts-1; i >= 0; --i) {
		*pd-- = (double)*pf--;
	    }
	}
	if (sz == 1) {
	    int i;
	    double *pd = mxGetPr(rslt);
	    char *pc = (char *)pd;
	    pd = pd + npts - 1;
	    pc = pc + npts - 1;
	    for(i = npts-1; i >= 0; --i) {
		*pd-- = (double)*pc--;
	    }
	}
	if (sz == 2) {
	    int i;
	    double *pd = mxGetPr(rslt);
	    short *ps = (short *)pd;
	    pd = pd + npts - 1;
	    ps = ps + npts - 1;
	    if (fmt == MXPO_INT16N) {
		for(i = npts-1; i >= 0; --i) {
		    *pd-- = (double)*ps--;
		}
	    } else { /* MXPO_INT16R */
		short v;
		for(i = npts-1; i >= 0; --i) {
		    v = (short)*pd--;
		    *ps -- = (0xFF & (v >> 8)) + (0xFF00 & (v << 8));
		}
	    }
	}
	
	/* did we get all the points we asked for */
	if (ngot < npts) {
	    /* allocate a smaller array and copy to that */
	    /* but only chop down by whole columns */
	    int gotcols,gotrows;
	    int i;
	    double *pd, *ps;
	    mxArray *newarr;
	    if (cols == 1) {
		gotrows = ngot;
		gotcols = 1;
	    } else {
		gotrows = rows;
		gotcols = (ngot+rows-1)/rows;
	    }
	    newarr = mxCreateDoubleMatrix(gotrows, gotcols, mxREAL);
	    pd = mxGetPr(newarr);
	    ps = mxGetPr(rslt);
	    for (i = 0; i < ngot; ++i) {
		*pd++ = *ps++;
	    }
	    for (i = ngot; i < gotrows*gotcols; ++i) {
		*pd++ = 0;
	    }
	    mxDestroyArray(rslt);
	    rslt = newarr;
	}

	if (nlhs > 0) {
	    plhs[0] = rslt;
	} else {
	    mxDestroyArray(rslt);
	}
	return;
    }