Path: news.mathworks.com!not-for-mail
From: "basudha pradhan" <lucky_faith_911@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: reading binary file from mexFunction
Date: Tue, 11 Sep 2007 17:58:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 63
Message-ID: <fc6kvd$1ul$1@fred.mathworks.com>
References: <fc1fri$au3$1@fred.mathworks.com> <fc1r8j$iql$1@fred.mathworks.com>
Reply-To: "basudha pradhan" <lucky_faith_911@hotmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1189533485 2005 172.30.248.37 (11 Sep 2007 17:58:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 11 Sep 2007 17:58:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1088565
Xref: news.mathworks.com comp.soft-sys.matlab:427911


Thanks Walter. I tried something similar to what you 
suggested. I can get the function to compile but when i ran 
it , it crashes. This is what i tried.

/* The gateway routine */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
   char *input_buf;
   uint8 *output_buf; 
   int buflen;
   int status;  
   FILE *pFile;
   long lSize;
   size_t result;  
   
  /* Output must be a string. */
  if (mxIsChar(plhs[0]) != 1)
    mexErrMsgTxt("Output must be a string.");

  /* Output must be a row vector. */
  if (mxGetM(plhs[0]) != 1)
    mexErrMsgTxt("Output must be a row vector."); 
  
  
  /* Allocate memory for output strings. */
  plhs[0] = mxCalloc(150000, sizeof(char)); 
  output_buf = mxGetPr(plhs[0]);
  
  
  
  /*Open binary file for read*/
  pFile = fopen ( "file.bin" , "rb" ); 
     if (pFile==NULL) mexErrMsgTxt("File cannot be 
opened.");

     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);

     // allocate memory to contain the whole file:
     plhs[1] = (uint8*) mxCalloc(lSize, sizeof(uint8));
     input_buf = mxGetPr(plhs[1]);

     // copy the file into the buffer:
     result = fread (input_buf,1,lSize,pFile);
     if (result != lSize) mexErrMsgTxt("incorrect 
reading."); 
     /* the whole file is now loaded in the memory buffer. 
*/
     fclose(pFile); 
      
    /* Call the C subroutine. */
  MQ_dec(input_buf,output_buf);
  mxFree(plhs[0]);
  mxFree(plhs[1]);
}

I would really appreciate your suggestions.

Thanks,
Basudha