Beispiel #1
0
int main(int argc, const char *argv[])

{
  Engine *ep;
  char buff[10240];
  int i;

  /* matlab must be in the PATH! */
  if (!(ep = engOpen("matlab -nodisplay"))) {
    fprintf(stderr, "Can't start MATLAB engine\n");
    return -1;
  }
  engOutputBuffer(ep, buff, 10239);

  /* load the mex file */
  if(argc<2){
    fprintf(stderr, "Error. Give full path to the MEX file as input parameter.\n");
    return -1;
  }
  void *handle = dlopen(argv[1], RTLD_NOW);
  if(!handle){
    fprintf(stderr, "Error loading MEX file: %s\n", strerror(errno));
    return -1;
  }

  /* grab mexFunction handle */
  mexFunction_t mexfunction = (mexFunction_t)dlsym(handle, "mexFunction");
  if(!mexfunction){
    fprintf(stderr, "MEX file does not contain mexFunction\n");
    return -1;
  }

  /* load input data - for convenience do that using MATLAB engine */
  /* NOTE: parameters are MEX-file specific, so one has to modify this*/
  /* to fit particular needs */
  engEvalString(ep, "load input.mat");
  mxArray *arg1 = engGetVariable(ep, "im1");
  mxArray *arg2 = engGetVariable(ep, "im2");
  mxArray *arg3 = engGetVariable(ep, "szx");
  mxArray *arg4 = engGetVariable(ep, "szy");
  mxArray *arg5 = engGetVariable(ep, "ngh");
  mxArray *pargout[1] = {0};
  const mxArray *pargin[5] = {arg1, arg2,arg3, arg4,arg5};

  /* execute the mex function */
  mexfunction(1, pargout, 5, pargin);

  /* print the results using MATLAB engine */
  engPutVariable(ep, "result", pargout[0]);
  engEvalString(ep, "result");
  printf("%s\n", buff);

  /* cleanup */
  mxDestroyArray(pargout[0]);
  engEvalString(ep, "clear all;");
  dlclose(handle);
  engClose(ep);

  return 0;
}
Beispiel #2
0
int main(int argc,char* argv[])
{

   if(argc<2)
   {
      fprintf(stderr,"Correct parameters: NAMEOFMEX.MEX ARGS.mat\n");
      return -1;
   }

   void* tmp = fftw_malloc(1);
   fftw_free(tmp);
   tmp = fftwf_malloc(1);
   fftwf_free(tmp);

   mexFunction_t mexfunction = NULL;
#if defined(_WIN32) || defined(__WIN32__)
// NOTE: When specifying a path, be sure to use backslashes (\), not forward slashes (/).
   HINSTANCE dllHandle = NULL;
   dllHandle = LoadLibrary((argv[1]));
   if(dllHandle==NULL){
      fprintf(stderr, "Error loading MEX file.\n");
      return -1;
   }

 /*
 Preferably use the mexFunctionInner, which skips registering mexAtExit which causes crashes.
 */
   mexfunction = (mexFunction_t)GetProcAddress(dllHandle,"mexFunctionInner");
   if(mexfunction==NULL){
      mexfunction = (mexFunction_t)GetProcAddress(dllHandle,"mexFunction");
      if(mexfunction==NULL){
         fprintf(stderr, "MEX file does not contain mexFunction\n");
         return -1;
      }
   }
#else
  void *handle = dlopen(argv[1], RTLD_NOW);
  if(!handle){
    fprintf(stderr, "Error loading MEX file: %s\n", strerror(errno));
    return -1;
  }

  mexfunction = (mexFunction_t)dlsym(handle, "mexFunctionInner");
  if(!mexfunction){
    mexfunction = (mexFunction_t)dlsym(handle, "mexFunction");
    if(!mexfunction){
       fprintf(stderr, "MEX file does not contain mexFunction\n");
       return -1;
    }
  }
#endif

   mxArray* res[1];
   res[0] = NULL;
   int argCount = getNoOfArgsFromMAT(argv[2]);
   const mxArray* prhs[argCount];
   getArgsFromMAT(argv[2],(mxArray**)prhs,(mxArray**)res, argCount);
   mxArray* plhs[1];


   if(res[0]!=NULL)
   {
       argCount--;
   }

   mexfunction(1,plhs,argCount,prhs);

   if(res!=NULL)
   {
     if(mxGetClassID(plhs[0])!=mxDOUBLE_CLASS)
     {
       fprintf(stderr, "Currently working with doubles only.\n");
       return -1;
     }
     double err=compMSE_d(res[0],plhs[0]);
     fprintf(stdout, "Error: %e\n", err);
     mxDestroyArray(res[0]);
   }


   for(int ii=0;ii<argCount;ii++)
      mxDestroyArray((mxArray*)prhs[ii]);

   mxDestroyArray(plhs[0]);

   //Free the library:
#if defined(_WIN32) || defined(__WIN32__)
   //  BOOL freeResult = FreeLibrary(dllHandle);
#else
   dlclose(handle);
#endif

}