void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  if(nrhs < 1)
    mexErrMsgTxt("At least one input argument is required.");
  if (nlhs > 1)
    mexErrMsgTxt("Too many output arguments.");
  
  btk::MergeAcquisitionFilter::Pointer merger = btk::MergeAcquisitionFilter::New();
  std::vector<btk::Acquisition::Pointer> acq(nrhs);
  for (int i = 0 ; i < nrhs ; ++i)
    merger->SetInput(i, btk_MOH_get_object<btk::Acquisition>(prhs[i]));

  // Redirection of the btk::Logger::Warning stream.
  btk::MEXWarnLogToWarnMsgTxt warnRedir = btk::MEXWarnLogToWarnMsgTxt("btk:MergeAcquisitions");
  
  merger->Update();
  
  plhs[0] = btk_MOH_create_handle(merger->GetOutput());
  
#if defined(__APPLE__) || (defined(BTK_BUILD_SHARED_LIBS) && defined(__unix__))
  // It seems to be related only to Linux with shared libraries
  // This fix was only tested with Matlab r2009a (7.8)
  // FIXME: This solution clear all the acquisitions and not only the ones 
  //        created from this function
  mexAtExit(btk::MEXHandleCollector<btk::Acquisition>::ManualClear);
#endif
};
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  if(nrhs != 1)
    mexErrMsgTxt("One input required.");
  if (nlhs > 3)
    mexErrMsgTxt("Too many output arguments.");

  if (!mxIsChar(prhs[0]) || mxIsEmpty(prhs[0]))
   mexErrMsgTxt("The filename must be a string and can't be empty.");

  // Redirection of the btk::Logger::Warning stream.
  btk::MEXWarnLogToWarnMsgTxt warnRedir = btk::MEXWarnLogToWarnMsgTxt("btk:ReadAcquisition");

  size_t strlen_ = (mxGetM(prhs[0]) * mxGetN(prhs[0]) * sizeof(mxChar)) + 1;
  char* filename = (char*)mxMalloc(strlen_);
  mxGetString(prhs[0], filename, strlen_); 
  
  btk::AcquisitionFileReader::Pointer reader = btk::AcquisitionFileReader::New();
  reader->SetFilename(std::string(filename));
  
  mxFree(filename);
  
  try
  {
    reader->Update();
  }
  catch(std::exception& e)
  {
    // Octave seems to not call the destructor of the btkSharedPtr when an exception is thrown (possible memory leak).
    reader.reset();
    mexErrMsgTxt(e.what());
  }
  catch(...)
  {
    reader.reset();
    mexErrMsgTxt("An unexpected error occurred.");
  }
  
  plhs[0] = btk_MOH_create_handle(reader->GetOutput());
  if (nlhs > 1) // Byte Order
    plhs[1] = mxCreateString(reader->GetAcquisitionIO()->GetByteOrderAsString().c_str());
  if (nlhs > 2) // Storage format
    plhs[2] = mxCreateString(reader->GetAcquisitionIO()->GetStorageFormatAsString().c_str());
  
#if defined(__APPLE__) || (defined(BTK_BUILD_SHARED_LIBS) && defined(__unix__))
  // It seems to be related only to Linux with shared libraries
  // This fix was only tested with Matlab r2009a (7.8)
  // FIXME: This solution clear all the acquisitions and not only the ones 
  //        created from this function
  mexAtExit(btk::MEXHandleCollector<btk::Acquisition>::ManualClear);
#endif
};
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  if(nrhs > 4)
    mexErrMsgTxt("Too many input arguments.");
  if (nlhs > 1)
    mexErrMsgTxt("Too many output arguments.");

  int pn = 0, fn = 0, an = 0, r = 1;
  
  for (int i = 0 ; i < nrhs ; ++i)
  {
    if ((mxGetClassID(prhs[i]) != mxDOUBLE_CLASS) || mxIsEmpty(prhs[i]) || mxIsComplex(prhs[i])
        || (mxGetNumberOfElements(prhs[i]) != 1))
      mexErrMsgTxt("All input arguments must be set to double values representing integers.");
  }
  if (nrhs >= 1)
    pn = static_cast<int>(mxGetScalar(prhs[0]));
  if (nrhs >= 2)
    fn = static_cast<int>(mxGetScalar(prhs[1]));
  if(nrhs >= 3)
    an = static_cast<int>(mxGetScalar(prhs[2]));
  if(nrhs >= 4)
    r = static_cast<int>(mxGetScalar(prhs[3]));
  
  if (r == 0)
    mexErrMsgTxt("Impossible to set the analog sample number by point frame to 0.");

  btk::Acquisition::Pointer acq = btk::Acquisition::New();
  acq->Init(pn, fn, an, r);
  plhs[0] = btk_MOH_create_handle(acq);
  
#if defined(__APPLE__) || (defined(BTK_BUILD_SHARED_LIBS) && defined(__unix__))
  // It seems to be related only to Linux with shared libraries
  // This fix was only tested with Matlab r2009a (7.8)
  // FIXME: This solution clear all the acquisitions and not only the ones 
  //        created from this function
  mexAtExit(btk::MEXHandleCollector<btk::Acquisition>::ManualClear);
#endif
};