#include #include #include #include "mex.h" /* Gateway Routine */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *ecg; short low,high,nleads; long Nc,Nb,i,sp,p; FILE *fp; char *nombre; unsigned char *buf; /* Compruebo el numero OK de los argumentos */ if (nrhs != 2){ mexErrMsgTxt("Ponme 2 argumentos de entrada."); return; } else if (nlhs > 1){ mexErrMsgTxt("Ponme 1 argumento de salida."); return; } /* Debo crear un matriz para el argumento de salida */ i = (long) mxGetN(prhs[0])+1; nombre = mxCalloc((int)i, sizeof(char)); mxGetString(prhs[0], nombre, i); Nc = mxGetM(prhs[1]); nleads = mxGetN(prhs[1]); ecg=mxGetPr(prhs[1]); Nb = 3*Nc; /* Number of bytes to write */ buf = (char *) malloc(Nb*sizeof(char)); if (buf == NULL) { printf("Error: insufficient memory\n"); return; } /* Computational Routine */ if((fp=fopen(nombre, "a")) == NULL){ printf("Error: Unable to open file %s.\n", nombre); return(1); } else { for (i=0,sp=0; i>8; high=(0X0F00 & (int) ecg[i+Nc])>>8; buf[sp+1]=0XFF & (low + high*16); buf[sp+2]=(0XFF & (int) ecg[i+Nc]); sp += 3 ; } p=fwrite(buf, sizeof(char), Nb, fp); fclose(fp); plhs[0]=&p; } mxFree(nombre); free(buf); }