Path: news.mathworks.com!newsfeed-00.mathworks.com!panix!bloom-beacon.mit.edu!llnews!53ab2750!not-for-mail
Newsgroups: comp.soft-sys.matlab
Subject: Re: reading binary file from mexFunction
References: <fc1fri$au3$1@fred.mathworks.com> <fc1r8j$iql$1@fred.mathworks.com> <fc6kvd$1ul$1@fred.mathworks.com>
From: Peter Boettcher <boettcher@ll.mit.edu>
Message-ID: <muymyvtnkj8.fsf@G99-Boettcher.llan.ll.mit.edu>
Organization: MIT Lincoln Laboratory
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (gnu/linux)
Cancel-Lock: sha1:2YSIvuG1EYf8m/xEqtIhcDFJEdM=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 79
Date: Tue, 11 Sep 2007 14:07:07 -0400
NNTP-Posting-Host: 155.34.163.114
X-Complaints-To: news@ll.mit.edu
X-Trace: llnews 1189533709 155.34.163.114 (Tue, 11 Sep 2007 14:01:49 EDT)
NNTP-Posting-Date: Tue, 11 Sep 2007 14:01:49 EDT
Xref: news.mathworks.com comp.soft-sys.matlab:427916


"basudha pradhan" <lucky_faith_911@hotmail.com> writes:

> 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.");

plhs[0] doesn't exist until you create it.

>   /* Output must be a row vector. */
>   if (mxGetM(plhs[0]) != 1)
>     mexErrMsgTxt("Output must be a row vector."); 

likewise...

>   /* Allocate memory for output strings. */
>   plhs[0] = mxCalloc(150000, sizeof(char)); 
>   output_buf = mxGetPr(plhs[0]);

You need to create plhs[0] to be an mxArray, not an arbitrary chunk of
memory.  So you need something like mxCreateArray() instead.

>   
>   /*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]);

Same here.  Except you also need to be sure plhs[1] exists, via nlhs

>      // 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]);

If you don't need to return the data to MATLAB, then don't use plhs[]
at all!  Just use a char* pointer, mxCalloc(), and mxFree().  So
scratch my advice above for this purpose.

If you DO want data returned to MATLAB, then create the arrays like I
recommend, then DON'T try to free them at the end.  You want them
delivered to MATLAB.

I can add a little more detail if you clarify what you are trying to
achieve...

-Peter