Index: /tools/matlabread/matlabread.cc
===================================================================
--- /tools/matlabread/matlabread.cc	(revision 101)
+++ /tools/matlabread/matlabread.cc	(revision 102)
@@ -4,5 +4,7 @@
 // Use 'mex matlabread.cc RawDataCTX.o' in Matlab to compile.
 // 
-// Oliver Grimm, July 2009
+// When called with an empy filename, the previously open data file will be used.
+//
+// Oliver Grimm, September 2009
 
 
@@ -14,8 +16,18 @@
 
 
+// static declaration keep variable alive over invocations of mex file
+static RawDataCTX *RD = NULL;
+
+// Exit function called when Matlab is terminated (RD==NULL is OK for delete)
+void ExitFcn() {
+  delete RD;
+}
+
 // Interface function to Matlab
 void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
+
+  mexAtExit(*ExitFcn); // Exit function to call when matlab terminates
   
-  RawDataCTX* RD = new RawDataCTX(true);	// Instantiate without console output
+  if (RD == NULL) RD = new RawDataCTX(true);	// Instantiate without console output
 
   // Check inputs and output arguments
@@ -23,19 +35,25 @@
     mexErrMsgTxt("Usage: [Data TrigCells RHeader BStructs EHeader] = matlabread('Filename', EventNo)");
   }
-          
-  switch (RD->OpenDataFile(mxArrayToString(prhs[0]), NULL)) {
-    case CTX_FOPEN:   mexErrMsgTxt("Could not open file.");
-    case CTX_RHEADER: mexErrMsgTxt("Could not read run header.");
-    case CTX_BSTRUCT: mexErrMsgTxt("Could not read board structures.");
+  
+  char *Filename = mxArrayToString(prhs[0]);
+  if (Filename == NULL) mexErrMsgTxt("Could not create filename string.");
+  
+  if (strlen(Filename) != 0) {
+    switch (RD->OpenDataFile(Filename), NULL) {
+      case CTX_FOPEN:   mexErrMsgTxt("Could not open file.");
+      case CTX_RHEADER: mexErrMsgTxt("Could not read run header.");
+      case CTX_BSTRUCT: mexErrMsgTxt("Could not read board structures.");
+    }
+    if (RD->RHeader->MagicNum == MAGICNUM_OPEN) {
+      mexWarnMsgTxt("Magic number in run header indicates that the file has not "
+    		    "been closed properly.");
+    }
+    if (RD->RHeader->MagicNum == MAGICNUM_ERROR) {
+      mexWarnMsgTxt("Magic number in run header indicates that an error occurred "
+    		    "while writing the file.");
+    }
   }
-  if (RD->RHeader->MagicNum == MAGICNUM_OPEN) {
-    mexWarnMsgTxt("Magic number in run header indicates that the file has not "
-    		  "been closed properly.");
-  }
-  if (RD->RHeader->MagicNum == MAGICNUM_ERROR) {
-    mexWarnMsgTxt("Magic number in run header indicates that an error occurred "
-    		  "while writing the file.");
-  }
-
+  mxFree(Filename);
+  
   // ...some abbrevation for convenience
   unsigned int Boards = RD->RHeader->NBoards;
@@ -44,11 +62,12 @@
   unsigned int Samples = RD->RHeader->Samples;
   
-  // Print header data
+  // Read event
   if(RD->ReadEvent(mxIsDouble(prhs[1]) ? (int) mxGetScalar(prhs[1]):0, NULL) != CTX_OK) {
     mexErrMsgTxt("Could not read event.");
   }
 
-  while(true) { // Dummy loop to allow different number of return values (break will be issued depending on nlhs)
-  
+  // Dummy loop to allow different return values (break will be issued depending on nlhs)
+  while(true) {
+
   // ========= Data array ==========
   
@@ -175,7 +194,4 @@
   if (nlhs>=5) break;
 
-  } // Dummy while loop
-  
-  
-  delete RD;   
+  } // Dummy while loop  
 }
