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> <muymyvtnkj8.fsf@G99-Boettcher.llan.ll.mit.edu> <fc7ai3$hu8$1@fred.mathworks.com>
From: Peter Boettcher <boettcher@ll.mit.edu>
Message-ID: <muyr6l4m1rn.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:v9hOk7HBfQmcESkGcwSdgTP58DY=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 69
Date: Wed, 12 Sep 2007 09:50:04 -0400
NNTP-Posting-Host: 155.34.163.114
X-Complaints-To: news@ll.mit.edu
X-Trace: llnews 1189604682 155.34.163.114 (Wed, 12 Sep 2007 09:44:42 EDT)
NNTP-Posting-Date: Wed, 12 Sep 2007 09:44:42 EDT
Xref: news.mathworks.com comp.soft-sys.matlab:428067


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

> Peter,
>
> I am trying to read content of binary file from a 
> mexfunction.My mexfunction doesn't have any input argument. 
> It just open up a binary file and copy the content of 
> binary file to input_buffer . Then, I would like  to call C 
> function that take this input_buffer as an input and 
> process it to give output(character array data types) which 
> i will store in output_buffer. So, i had plhs[0] pointed to 
> output buffer that will be passed back to Matlab. My 
> output_buffer is not of fixed length. It may vary depending 
> upon different content of binary file. So how do i declare 
> mxCreateCharArray() of variable length.
> mxArray *mxCreateCharArray(int ndim, const int *dims);
> what will be ndim and *dim if they vary for different inputs
> (its not the same size as input). this is what i got till 
> now. Your suggestions will be greatly appreciated.

input_buf is now exactly correct.

You are on the right track with mxCreateCharArray().  You need to know
what size array you want as you create it.  You might move this
creation below the calculation of lSize, then you can just use that
(if that's the size you want!).

Other comments below.

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

plhs[0] does not exist until you create it.  Remove the above 2 tests,
they are not necessary, and will probably break anyway.

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

Replace the above section with:

int char_dims[2];
char_dims[0] = 1;  /* First dimension: number of rows */
char_dims[1] = 150000; /* Second dimension: number of columns */
plhs[0] = mxCreateCharArray(2, char_dims);
output_buf = mxGetPr(plhs[0]);

Aside from putting this segment in a place where you know what size
you want, the rest looks fine.


-Peter