<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838</link>
    <title>MATLAB Central Newsreader - reading binary file from mexFunction</title>
    <description>Feed for thread: reading binary file from mexFunction</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 12 Sep 2007 20:55:48 -0400</pubDate>
      <title>Re: reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391962</link>
      <author>Peter Boettcher</author>
      <description>"basudha pradhan" &amp;lt;lucky_faith_911@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; Thanks Peter. It works now. That mex function was for &lt;br&gt;
&amp;gt; decoder. I have mex function for encoder . The encoder runs &lt;br&gt;
&amp;gt; ok in C but when i call it from matlab, the encoded file is &lt;br&gt;
&amp;gt; not right. So i am guessing my mex function is bad. &lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; input to the mex function is char array(input_buf). then it &lt;br&gt;
&amp;gt; call C subroutine where it pass this input_buf and buflen&lt;br&gt;
&amp;gt; (length of input). C subroutine process the input and write &lt;br&gt;
&amp;gt; the output to a file. So there is no output variable for &lt;br&gt;
&amp;gt; mex function. this is what i tried. &lt;br&gt;
&lt;br&gt;
OK, makes sense.&lt;br&gt;
&lt;br&gt;
&amp;gt; void mexFunction(int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;gt;                  int nrhs, const mxArray *prhs[])&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;    char *input_buf; &lt;br&gt;
&amp;gt;    int buflen;&lt;br&gt;
&amp;gt;    int status;  &lt;br&gt;
&amp;gt;      &lt;br&gt;
&amp;gt;    /* Check for proper number of arguments. */&lt;br&gt;
&amp;gt;   if (nrhs != 1) &lt;br&gt;
&amp;gt;     mexErrMsgTxt("One input required.");&lt;br&gt;
&amp;gt;   &lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;   /* Input must be a string. */&lt;br&gt;
&amp;gt;   if (mxIsChar(prhs[0]) != 1)&lt;br&gt;
&amp;gt;     mexErrMsgTxt("Input must be a string.");&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;   /* Input must be a row vector. */&lt;br&gt;
&amp;gt;   if (mxGetM(prhs[0]) != 1)&lt;br&gt;
&amp;gt;     mexErrMsgTxt("Input must be a row vector.");&lt;br&gt;
&lt;br&gt;
All good.&lt;br&gt;
&lt;br&gt;
&amp;gt;   /* Get the length of the input string. */&lt;br&gt;
&amp;gt;   buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;&lt;br&gt;
&lt;br&gt;
Good.  mxGetNumberOfElements() is a shortcut.&lt;br&gt;
&lt;br&gt;
&amp;gt;   /* Allocate memory for input strings. */&lt;br&gt;
&amp;gt;   input_buf = mxCalloc(buflen, sizeof(char));&lt;br&gt;
&amp;gt;   &lt;br&gt;
&amp;gt;   /* Copy the string data from prhs[0] into a C string &lt;br&gt;
&amp;gt;    * input_buf. */&lt;br&gt;
&amp;gt;   status = mxGetString(prhs[0], input_buf, buflen);&lt;br&gt;
&lt;br&gt;
OK.  MATLAB strings are funny.  They are stored internally as 16-bit,&lt;br&gt;
which is why you use mxGetString.  In fact, I'd say this should work.&lt;br&gt;
But, since what you probably really want to send is 8-bit ASCII&lt;br&gt;
characters, the whole thing will be more efficient (and may work!) if&lt;br&gt;
you cast the MATLAB string to an explicit integer type: uint8.&lt;br&gt;
&lt;br&gt;
Then, you can use mxGetData(prhs[0]) just like mxGetPr() to grab a&lt;br&gt;
pointer to the actual data, and process directly from there.  That&lt;br&gt;
means skip mxCalloc, mxGetString, etc.&lt;br&gt;
&lt;br&gt;
&amp;gt;   if (status != 0) &lt;br&gt;
&amp;gt;     mexWarnMsgTxt("Not enough space. String is &lt;br&gt;
&amp;gt; truncated.");    &lt;br&gt;
&amp;gt;       &lt;br&gt;
&amp;gt;     /* Call the C subroutine. */&lt;br&gt;
&amp;gt;   MQ_enc(input_buf, buflen);&lt;br&gt;
&amp;gt;   mxFree(input_buf);&lt;br&gt;
&amp;gt; }&lt;br&gt;
&lt;br&gt;
You might want to do some debugging by dumping the first and last 16&lt;br&gt;
bytes of your input_buf before you call MQ_enc, and compare that to&lt;br&gt;
your straight C version.  Or even dump the full contents of input_buf&lt;br&gt;
to a file in both places and compare.&lt;br&gt;
&lt;br&gt;
-Peter&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 12 Sep 2007 20:42:36 -0400</pubDate>
      <title>Re: reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391961</link>
      <author>basudha pradhan</author>
      <description>Thanks Peter. It works now. That mex function was for &lt;br&gt;
decoder. I have mex function for encoder . The encoder runs &lt;br&gt;
ok in C but when i call it from matlab, the encoded file is &lt;br&gt;
not right. So i am guessing my mex function is bad. &lt;br&gt;
&lt;br&gt;
input to the mex function is char array(input_buf). then it &lt;br&gt;
call C subroutine where it pass this input_buf and buflen&lt;br&gt;
(length of input). C subroutine process the input and write &lt;br&gt;
the output to a file. So there is no output variable for &lt;br&gt;
mex function. this is what i tried. &lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int nrhs, const mxArray *prhs[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;char *input_buf; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int buflen;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int status;  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Check for proper number of arguments. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (nrhs != 1) &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt("One input required.");&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Input must be a string. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (mxIsChar(prhs[0]) != 1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt("Input must be a string.");&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Input must be a row vector. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (mxGetM(prhs[0]) != 1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt("Input must be a row vector.");&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Get the length of the input string. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Allocate memory for input strings. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;input_buf = mxCalloc(buflen, sizeof(char));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Copy the string data from prhs[0] into a C string &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;* input_buf. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;status = mxGetString(prhs[0], input_buf, buflen);&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (status != 0) &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexWarnMsgTxt("Not enough space. String is &lt;br&gt;
truncated.");    &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Call the C subroutine. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;MQ_enc(input_buf, buflen);&lt;br&gt;
&amp;nbsp;&amp;nbsp;mxFree(input_buf);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
Your suggestions would be greatly appreciated.&lt;br&gt;
&lt;br&gt;
Thanks&lt;br&gt;
~Basudha&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 12 Sep 2007 13:50:04 -0400</pubDate>
      <title>Re: reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391847</link>
      <author>Peter Boettcher</author>
      <description>"basudha pradhan" &amp;lt;lucky_faith_911@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; Peter,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am trying to read content of binary file from a &lt;br&gt;
&amp;gt; mexfunction.My mexfunction doesn't have any input argument. &lt;br&gt;
&amp;gt; It just open up a binary file and copy the content of &lt;br&gt;
&amp;gt; binary file to input_buffer . Then, I would like  to call C &lt;br&gt;
&amp;gt; function that take this input_buffer as an input and &lt;br&gt;
&amp;gt; process it to give output(character array data types) which &lt;br&gt;
&amp;gt; i will store in output_buffer. So, i had plhs[0] pointed to &lt;br&gt;
&amp;gt; output buffer that will be passed back to Matlab. My &lt;br&gt;
&amp;gt; output_buffer is not of fixed length. It may vary depending &lt;br&gt;
&amp;gt; upon different content of binary file. So how do i declare &lt;br&gt;
&amp;gt; mxCreateCharArray() of variable length.&lt;br&gt;
&amp;gt; mxArray *mxCreateCharArray(int ndim, const int *dims);&lt;br&gt;
&amp;gt; what will be ndim and *dim if they vary for different inputs&lt;br&gt;
&amp;gt; (its not the same size as input). this is what i got till &lt;br&gt;
&amp;gt; now. Your suggestions will be greatly appreciated.&lt;br&gt;
&lt;br&gt;
input_buf is now exactly correct.&lt;br&gt;
&lt;br&gt;
You are on the right track with mxCreateCharArray().  You need to know&lt;br&gt;
what size array you want as you create it.  You might move this&lt;br&gt;
creation below the calculation of lSize, then you can just use that&lt;br&gt;
(if that's the size you want!).&lt;br&gt;
&lt;br&gt;
Other comments below.&lt;br&gt;
&lt;br&gt;
&amp;gt; void mexFunction(int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;gt;                  int nrhs, const mxArray *prhs[])&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;    char *input_buf;&lt;br&gt;
&amp;gt;    uint8 *output_buf; &lt;br&gt;
&amp;gt;    int buflen;&lt;br&gt;
&amp;gt;    int status;  &lt;br&gt;
&amp;gt;    FILE *pFile;&lt;br&gt;
&amp;gt;    long lSize;&lt;br&gt;
&amp;gt;    size_t result;  &lt;br&gt;
&amp;gt;    &lt;br&gt;
&amp;gt;   /* Output must be a string. */&lt;br&gt;
&amp;gt;   if (mxIsChar(plhs[0]) != 1)&lt;br&gt;
&amp;gt;     mexErrMsgTxt("Output must be a string.");&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;   /* Output must be a row vector. */&lt;br&gt;
&amp;gt;   if (mxGetM(plhs[0]) != 1)&lt;br&gt;
&amp;gt;     mexErrMsgTxt("Output must be a row vector."); &lt;br&gt;
&lt;br&gt;
plhs[0] does not exist until you create it.  Remove the above 2 tests,&lt;br&gt;
they are not necessary, and will probably break anyway.&lt;br&gt;
&lt;br&gt;
&amp;gt;   /* Allocate memory for output strings. */&lt;br&gt;
&amp;gt;   plhs[0] = mxCalloc(150000, sizeof(char)); &lt;br&gt;
&amp;gt; //not sure how to use mxCreateCharArray()&lt;br&gt;
&amp;gt;   output_buf = mxGetPr(plhs[0]);&lt;br&gt;
&lt;br&gt;
Replace the above section with:&lt;br&gt;
&lt;br&gt;
int char_dims[2];&lt;br&gt;
char_dims[0] = 1;  /* First dimension: number of rows */&lt;br&gt;
char_dims[1] = 150000; /* Second dimension: number of columns */&lt;br&gt;
plhs[0] = mxCreateCharArray(2, char_dims);&lt;br&gt;
output_buf = mxGetPr(plhs[0]);&lt;br&gt;
&lt;br&gt;
Aside from putting this segment in a place where you know what size&lt;br&gt;
you want, the rest looks fine.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-Peter&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 12 Sep 2007 00:06:27 -0400</pubDate>
      <title>Re: reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391760</link>
      <author>basudha pradhan</author>
      <description>Peter,&lt;br&gt;
&lt;br&gt;
I am trying to read content of binary file from a &lt;br&gt;
mexfunction.My mexfunction doesn't have any input argument. &lt;br&gt;
It just open up a binary file and copy the content of &lt;br&gt;
binary file to input_buffer . Then, I would like  to call C &lt;br&gt;
function that take this input_buffer as an input and &lt;br&gt;
process it to give output(character array data types) which &lt;br&gt;
i will store in output_buffer. So, i had plhs[0] pointed to &lt;br&gt;
output buffer that will be passed back to Matlab. My &lt;br&gt;
output_buffer is not of fixed length. It may vary depending &lt;br&gt;
upon different content of binary file. So how do i declare &lt;br&gt;
mxCreateCharArray() of variable length.&lt;br&gt;
mxArray *mxCreateCharArray(int ndim, const int *dims);&lt;br&gt;
what will be ndim and *dim if they vary for different inputs&lt;br&gt;
(its not the same size as input). this is what i got till &lt;br&gt;
now. Your suggestions will be greatly appreciated.&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int nrhs, const mxArray *prhs[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;char *input_buf;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;uint8 *output_buf; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int buflen;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int status;  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;FILE *pFile;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;long lSize;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;size_t result;  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Output must be a string. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (mxIsChar(plhs[0]) != 1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt("Output must be a string.");&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Output must be a row vector. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (mxGetM(plhs[0]) != 1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt("Output must be a row vector."); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Allocate memory for output strings. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;plhs[0] = mxCalloc(150000, sizeof(char)); &lt;br&gt;
//not sure how to use mxCreateCharArray()&lt;br&gt;
&amp;nbsp;&amp;nbsp;output_buf = mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/*Open binary file for read*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;pFile = fopen ( "file.bin" , "rb" ); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (pFile==NULL) mexErrMsgTxt("File cannot be &lt;br&gt;
opened.");&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// obtain file size:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fseek (pFile , 0 , SEEK_END);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lSize = ftell (pFile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rewind (pFile);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// allocate memory to contain the whole file:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;input_buf = (uint8*) mxCalloc(lSize, sizeof(uint8));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// copy the file into the buffer:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result = fread (input_buf,1,lSize,pFile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (result != lSize) mexErrMsgTxt("incorrect &lt;br&gt;
reading."); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* the whole file is now loaded in the memory buffer. &lt;br&gt;
*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fclose(pFile); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Call the C subroutine. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;MQ_dec(input_buf,output_buf);&lt;br&gt;
&amp;nbsp;&amp;nbsp;mxFree(input_buf);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Thanks&lt;br&gt;
~Basudha&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 11 Sep 2007 18:07:07 -0400</pubDate>
      <title>Re: reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391696</link>
      <author>Peter Boettcher</author>
      <description>"basudha pradhan" &amp;lt;lucky_faith_911@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; Thanks Walter. I tried something similar to what you &lt;br&gt;
&amp;gt; suggested. I can get the function to compile but when i ran &lt;br&gt;
&amp;gt; it , it crashes. This is what i tried.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; /* The gateway routine */&lt;br&gt;
&amp;gt; void mexFunction(int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;gt;                  int nrhs, const mxArray *prhs[])&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;    char *input_buf;&lt;br&gt;
&amp;gt;    uint8 *output_buf; &lt;br&gt;
&amp;gt;    int buflen;&lt;br&gt;
&amp;gt;    int status;  &lt;br&gt;
&amp;gt;    FILE *pFile;&lt;br&gt;
&amp;gt;    long lSize;&lt;br&gt;
&amp;gt;    size_t result;  &lt;br&gt;
&amp;gt;    &lt;br&gt;
&amp;gt;   /* Output must be a string. */&lt;br&gt;
&amp;gt;   if (mxIsChar(plhs[0]) != 1)&lt;br&gt;
&amp;gt;     mexErrMsgTxt("Output must be a string.");&lt;br&gt;
&lt;br&gt;
plhs[0] doesn't exist until you create it.&lt;br&gt;
&lt;br&gt;
&amp;gt;   /* Output must be a row vector. */&lt;br&gt;
&amp;gt;   if (mxGetM(plhs[0]) != 1)&lt;br&gt;
&amp;gt;     mexErrMsgTxt("Output must be a row vector."); &lt;br&gt;
&lt;br&gt;
likewise...&lt;br&gt;
&lt;br&gt;
&amp;gt;   /* Allocate memory for output strings. */&lt;br&gt;
&amp;gt;   plhs[0] = mxCalloc(150000, sizeof(char)); &lt;br&gt;
&amp;gt;   output_buf = mxGetPr(plhs[0]);&lt;br&gt;
&lt;br&gt;
You need to create plhs[0] to be an mxArray, not an arbitrary chunk of&lt;br&gt;
memory.  So you need something like mxCreateArray() instead.&lt;br&gt;
&lt;br&gt;
&amp;gt;   &lt;br&gt;
&amp;gt;   /*Open binary file for read*/&lt;br&gt;
&amp;gt;   pFile = fopen ( "file.bin" , "rb" ); &lt;br&gt;
&amp;gt;      if (pFile==NULL) mexErrMsgTxt("File cannot be &lt;br&gt;
&amp;gt; opened.");&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;      // obtain file size:&lt;br&gt;
&amp;gt;      fseek (pFile , 0 , SEEK_END);&lt;br&gt;
&amp;gt;      lSize = ftell (pFile);&lt;br&gt;
&amp;gt;      rewind (pFile);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;      // allocate memory to contain the whole file:&lt;br&gt;
&amp;gt;      plhs[1] = (uint8*) mxCalloc(lSize, sizeof(uint8));&lt;br&gt;
&amp;gt;      input_buf = mxGetPr(plhs[1]);&lt;br&gt;
&lt;br&gt;
Same here.  Except you also need to be sure plhs[1] exists, via nlhs&lt;br&gt;
&lt;br&gt;
&amp;gt;      // copy the file into the buffer:&lt;br&gt;
&amp;gt;      result = fread (input_buf,1,lSize,pFile);&lt;br&gt;
&amp;gt;      if (result != lSize) mexErrMsgTxt("incorrect &lt;br&gt;
&amp;gt; reading."); &lt;br&gt;
&amp;gt;      /* the whole file is now loaded in the memory buffer. &lt;br&gt;
&amp;gt; */&lt;br&gt;
&amp;gt;      fclose(pFile); &lt;br&gt;
&amp;gt;       &lt;br&gt;
&amp;gt;     /* Call the C subroutine. */&lt;br&gt;
&amp;gt;   MQ_dec(input_buf,output_buf);&lt;br&gt;
&amp;gt;   mxFree(plhs[0]);&lt;br&gt;
&amp;gt;   mxFree(plhs[1]);&lt;br&gt;
&lt;br&gt;
If you don't need to return the data to MATLAB, then don't use plhs[]&lt;br&gt;
at all!  Just use a char* pointer, mxCalloc(), and mxFree().  So&lt;br&gt;
scratch my advice above for this purpose.&lt;br&gt;
&lt;br&gt;
If you DO want data returned to MATLAB, then create the arrays like I&lt;br&gt;
recommend, then DON'T try to free them at the end.  You want them&lt;br&gt;
delivered to MATLAB.&lt;br&gt;
&lt;br&gt;
I can add a little more detail if you clarify what you are trying to&lt;br&gt;
achieve...&lt;br&gt;
&lt;br&gt;
-Peter&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 11 Sep 2007 17:58:05 -0400</pubDate>
      <title>reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391691</link>
      <author>basudha pradhan</author>
      <description>Thanks Walter. I tried something similar to what you &lt;br&gt;
suggested. I can get the function to compile but when i ran &lt;br&gt;
it , it crashes. This is what i tried.&lt;br&gt;
&lt;br&gt;
/* The gateway routine */&lt;br&gt;
void mexFunction(int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int nrhs, const mxArray *prhs[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;char *input_buf;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;uint8 *output_buf; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int buflen;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;int status;  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;FILE *pFile;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;long lSize;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;size_t result;  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Output must be a string. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (mxIsChar(plhs[0]) != 1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt("Output must be a string.");&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Output must be a row vector. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (mxGetM(plhs[0]) != 1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt("Output must be a row vector."); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Allocate memory for output strings. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;plhs[0] = mxCalloc(150000, sizeof(char)); &lt;br&gt;
&amp;nbsp;&amp;nbsp;output_buf = mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/*Open binary file for read*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;pFile = fopen ( "file.bin" , "rb" ); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (pFile==NULL) mexErrMsgTxt("File cannot be &lt;br&gt;
opened.");&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// obtain file size:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fseek (pFile , 0 , SEEK_END);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lSize = ftell (pFile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rewind (pFile);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// allocate memory to contain the whole file:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plhs[1] = (uint8*) mxCalloc(lSize, sizeof(uint8));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;input_buf = mxGetPr(plhs[1]);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// copy the file into the buffer:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result = fread (input_buf,1,lSize,pFile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (result != lSize) mexErrMsgTxt("incorrect &lt;br&gt;
reading."); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* the whole file is now loaded in the memory buffer. &lt;br&gt;
*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fclose(pFile); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Call the C subroutine. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;MQ_dec(input_buf,output_buf);&lt;br&gt;
&amp;nbsp;&amp;nbsp;mxFree(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;mxFree(plhs[1]);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
I would really appreciate your suggestions.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Basudha&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sun, 09 Sep 2007 22:14:43 -0400</pubDate>
      <title>reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391363</link>
      <author>Walter Roberson</author>
      <description>"basudha pradhan" &amp;lt;lucky_faith_911@hotmail.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;fc1fri$au3$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; how to read binary file from mexFunction and pass entire &lt;br&gt;
&amp;gt; content of file to some variable buffer? is there something &lt;br&gt;
&amp;gt; similar to fread() in mex function?&lt;br&gt;
&lt;br&gt;
There is something -very- much like fread() in mex functions.&lt;br&gt;
&lt;br&gt;
Use mxCreateDoubleArray (or as appropriate for the&lt;br&gt;
type of the data to read in.) Use mxGetData on the result&lt;br&gt;
of that in order to get a C pointer to the actual data&lt;br&gt;
block. Use C's fopen() to open the file, and use&lt;br&gt;
C's fread() to read the data into the area returned by&lt;br&gt;
mxGetData. When you are done with the file, use C's fclose().&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
(Note: This information is based upon browsing through the&lt;br&gt;
online documentation, not upon experience.)&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Sun, 09 Sep 2007 19:00:02 -0400</pubDate>
      <title>reading binary file from mexFunction</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155838#391337</link>
      <author>basudha pradhan</author>
      <description>how to read binary file from mexFunction and pass entire &lt;br&gt;
content of file to some variable buffer? is there something &lt;br&gt;
similar to fread() in mex function?&lt;br&gt;
&lt;br&gt;
Thanks in advance&lt;br&gt;
Basudha&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
