Changeset 102 for tools/matlabread


Ignore:
Timestamp:
09/18/09 12:17:47 (15 years ago)
Author:
ogrimm
Message:
Calling matlabread with empty filename allows fast browsing through previously opened file
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/matlabread/matlabread.cc

    r88 r102  
    44// Use 'mex matlabread.cc RawDataCTX.o' in Matlab to compile.
    55//
    6 // Oliver Grimm, July 2009
     6// When called with an empy filename, the previously open data file will be used.
     7//
     8// Oliver Grimm, September 2009
    79
    810
     
    1416
    1517
     18// static declaration keep variable alive over invocations of mex file
     19static RawDataCTX *RD = NULL;
     20
     21// Exit function called when Matlab is terminated (RD==NULL is OK for delete)
     22void ExitFcn() {
     23  delete RD;
     24}
     25
    1626// Interface function to Matlab
    1727void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
     28
     29  mexAtExit(*ExitFcn); // Exit function to call when matlab terminates
    1830 
    19   RawDataCTX* RD = new RawDataCTX(true);        // Instantiate without console output
     31  if (RD == NULL) RD = new RawDataCTX(true);    // Instantiate without console output
    2032
    2133  // Check inputs and output arguments
     
    2335    mexErrMsgTxt("Usage: [Data TrigCells RHeader BStructs EHeader] = matlabread('Filename', EventNo)");
    2436  }
    25          
    26   switch (RD->OpenDataFile(mxArrayToString(prhs[0]), NULL)) {
    27     case CTX_FOPEN:   mexErrMsgTxt("Could not open file.");
    28     case CTX_RHEADER: mexErrMsgTxt("Could not read run header.");
    29     case CTX_BSTRUCT: mexErrMsgTxt("Could not read board structures.");
     37 
     38  char *Filename = mxArrayToString(prhs[0]);
     39  if (Filename == NULL) mexErrMsgTxt("Could not create filename string.");
     40 
     41  if (strlen(Filename) != 0) {
     42    switch (RD->OpenDataFile(Filename), NULL) {
     43      case CTX_FOPEN:   mexErrMsgTxt("Could not open file.");
     44      case CTX_RHEADER: mexErrMsgTxt("Could not read run header.");
     45      case CTX_BSTRUCT: mexErrMsgTxt("Could not read board structures.");
     46    }
     47    if (RD->RHeader->MagicNum == MAGICNUM_OPEN) {
     48      mexWarnMsgTxt("Magic number in run header indicates that the file has not "
     49                    "been closed properly.");
     50    }
     51    if (RD->RHeader->MagicNum == MAGICNUM_ERROR) {
     52      mexWarnMsgTxt("Magic number in run header indicates that an error occurred "
     53                    "while writing the file.");
     54    }
    3055  }
    31   if (RD->RHeader->MagicNum == MAGICNUM_OPEN) {
    32     mexWarnMsgTxt("Magic number in run header indicates that the file has not "
    33                   "been closed properly.");
    34   }
    35   if (RD->RHeader->MagicNum == MAGICNUM_ERROR) {
    36     mexWarnMsgTxt("Magic number in run header indicates that an error occurred "
    37                   "while writing the file.");
    38   }
    39 
     56  mxFree(Filename);
     57 
    4058  // ...some abbrevation for convenience
    4159  unsigned int Boards = RD->RHeader->NBoards;
     
    4462  unsigned int Samples = RD->RHeader->Samples;
    4563 
    46   // Print header data
     64  // Read event
    4765  if(RD->ReadEvent(mxIsDouble(prhs[1]) ? (int) mxGetScalar(prhs[1]):0, NULL) != CTX_OK) {
    4866    mexErrMsgTxt("Could not read event.");
    4967  }
    5068
    51   while(true) { // Dummy loop to allow different number of return values (break will be issued depending on nlhs)
    52  
     69  // Dummy loop to allow different return values (break will be issued depending on nlhs)
     70  while(true) {
     71
    5372  // ========= Data array ==========
    5473 
     
    175194  if (nlhs>=5) break;
    176195
    177   } // Dummy while loop
    178  
    179  
    180   delete RD;   
     196  } // Dummy while loop 
    181197}
Note: See TracChangeset for help on using the changeset viewer.