Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!news.glorb.com!cycny01.gnilink.net!spamkiller2.gnilink.net!gnilink.net!trndny05.POSTED!702e7bde!not-for-mail
From: James Tursa <aclassyguywithaknotac@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Pass structure to MATLAB
Message-ID: <fggti3tfvrhmqmeomu45mklfgh9mf11uhf@4ax.com>
References: <fgb6fp$8rt$1@fred.mathworks.com> <6p9li3t0t4mc554emmf7q74rd5iut85d5l@4ax.com> <fgef3h$qrv$1@fred.mathworks.com> <70jmi39c96s8iudh4r1ajrrto95r873uak@4ax.com> <fggni2$4sk$1@fred.mathworks.com> <gr9oi31ibje2d1608d9b53kef8a5kr8hvk@4ax.com> <fghqqr$5br$1@fred.mathworks.com> <8mbpi3t46ho1q6svitnckpscnj8o7n85oi@4ax.com> <fgk30j$jau$1@fred.mathworks.com> <lptri39i3da708skv77eju752ah8gf47c9@4ax.com> <fglmjv$mc0$1@fred.mathworks.com>
X-Newsreader: Forte Agent 3.3/32.846
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 141
Date: Mon, 05 Nov 2007 07:15:57 GMT
NNTP-Posting-Host: 71.112.89.130
X-Complaints-To: abuse@verizon.net
X-Trace: trndny05 1194246957 71.112.89.130 (Mon, 05 Nov 2007 02:15:57 EST)
NNTP-Posting-Date: Mon, 05 Nov 2007 02:15:57 EST
Xref: news.mathworks.com comp.soft-sys.matlab:436044


On Mon, 5 Nov 2007 00:03:11 +0000 (UTC), "Faisal "
<faisal_mufti.nospam@yahoo.com> wrote:

>Hi
>> 1) Do you want to pass f.dist and f.ampl and u.dist and
>>u.ampl?  i.e.,do you want to pass all of them? Or just one
>>of the versions, either> the double or the short?
>
>Only f.ampl and f.dist. 
>
>> 2) Do you want to pass them in addition to the structures
>>that are already passed, or in place of the structures that
>>are already passed?
>
>No only them, not the structure
>
>>Note that once the structures are in the MATLAB workspace,
>>you can get at the individual arrays easily. For example,
>>using the current code I
>> just supplied:
> 
>I know that I can access the structured variables, but my
>code works better in a non structured way as I have to do
>some manipulation like 'find' which does not work on cells
>so I again have to put loops in Matlab which will slow down
>my processing time.
>
>> Finally, note that I have assumed that an unsigned short
>>in C was a 16-bit quantity. This isn't strictly guaranteed
>>in C. You are really only guaranteed that a short is at
>>least 16 bits, but it could be longer. To be more robust
>>one would check sizeof(short) and then use
>> an mxUINT16_CLASS or an mxUINT32_CLASS accordingly.
>
>Thanks, I will see to it.
>
>Regards,
>Faisal


Here it is. I deleted the first double argument which you were not
using. There is only one input argument, the filename. The double
versions of dist and ampl are the two output arguments.

James Tursa



#include "mex.h"
#include "matrix.h"
#include <stdlib.h>
#include <string.h>

#define ARRAYSIZE 3072

#ifndef mwSize
#define mwSize int
#endif

#ifndef mwIndex
#define mwIndex int
#endif

struct Frame
{
     double frame_no;
     double dist[ARRAYSIZE];
     double ampl[ARRAYSIZE];
};

struct UShortFrame
{
     unsigned frame_no;
     unsigned short dist[ARRAYSIZE];
     unsigned short ampl[ARRAYSIZE];
};

mxArray *DoubleTomxArray(double *dp, int dn);

void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
     const mxArray *prhs[])
{
    struct Frame *f;
    struct UShortFrame *u;
    size_t t1 = 1;
    char *filename;

    if( nrhs != 1 )
        mexErrMsgTxt("Must have exactly one input argument\n");
    if( nlhs > 2 )
        mexErrMsgTxt("Must have <= two output arguments\n");
    if( !mxIsChar(prhs[0]) )
        mexErrMsgTxt("Input argument must be char\n");
    
    filename = mxArrayToString(prhs[0]);

    ifstream filein(filename, ios::in | ios::binary);   
    mxFree(filename);
        
    if( !filein )
        mexErrMsgTxt("Cannot open file to read.\n");
           
    u = (struct UShortFrame *) mxCalloc( t1, sizeof(struct
UShortFrame) ); 
    f = (struct Frame *) mxCalloc( t1, sizeof(struct Frame) );

    (filein.read((char*)u,sizeof(struct UShortFrame)));
     
    {
            f->frame_no = u->frame_no;
            for (int i = 0; i < ARRAYSIZE; ++i)
               {
                    f->dist[i]=u->dist[i];
                    f->ampl[i]=u->ampl[i];
               }
    }

    filein.close();
   
    plhs[0] = DoubleTomxArray(f->dist, ARRAYSIZE);
    if( nlhs == 2 )
        plhs[1] = DoubleTomxArray(f->ampl, ARRAYSIZE);

    mxFree(f);
    mxFree(u);
}

// Convert a double array to an mxArray

mxArray *DoubleTomxArray(double *dp, int dn)
{
    mxArray *mx;
    mwSize m, n;
   
    m = 1;
    n = dn;
    mx = mxCreateNumericMatrix(m, n, mxDOUBLE_CLASS, mxREAL);
    memcpy(mxGetPr(mx), dp, sizeof(double)*dn);
    
    return mx;
}