Path: news.mathworks.com!newsfeed-00.mathworks.com!newscon02.news.prodigy.net!prodigy.net!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!cyclone1.gnilink.net!spamkiller2.gnilink.net!gnilink.net!trndny09.POSTED!702e7bde!not-for-mail
From: James Tursa <aclassyguywithaknotac@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Pass structure to MATLAB
Message-ID: <8mbpi3t46ho1q6svitnckpscnj8o7n85oi@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>
X-Newsreader: Forte Agent 3.3/32.846
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 175
Date: Sat, 03 Nov 2007 17:35:52 GMT
NNTP-Posting-Host: 71.112.98.49
X-Complaints-To: abuse@verizon.net
X-Trace: trndny09 1194111352 71.112.98.49 (Sat, 03 Nov 2007 13:35:52 EDT)
NNTP-Posting-Date: Sat, 03 Nov 2007 13:35:52 EDT
Bytes: 5594
X-Original-Bytes: 5551
Xref: news.mathworks.com comp.soft-sys.matlab:435930


On Sat, 3 Nov 2007 12:50:35 +0000 (UTC), "Faisal "
<faisal_mufti.nospam@yahoo.com> wrote:

>Yes, like that.
>Thanks


Try this. I couldn't test it because I don't have your file. One note,
however, is that you don't use the first input argument for anything
so I don't know why you are passing it in, but I left the code there
because you wrote it that way.

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 *FrameTomxArray(struct Frame *f);
mxArray *UShortFrameTomxArray(struct UShortFrame *u);

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

    if( nrhs != 2 )
        mexErrMsgTxt("Must have exactly two input arguments\n");
    if( nlhs > 2 )
        mexErrMsgTxt("Must have <= two output arguments\n");
    if( !mxIsDouble(prhs[0]) )
        mexErrMsgTxt("1st input argument must be double\n");
    if( !mxIsChar(prhs[1]) )
        mexErrMsgTxt("2nd input argument must be char\n");
    
// Why are you doing this? You don't use mrows, ncols, or f_count
anywhere
    mrows = mxGetM(prhs[0]);
    ncols = mxGetN(prhs[0]);
    f_count = mxGetPr(prhs[0]);

    filename = mxArrayToString(prhs[1]);

    ifstream filein(filename, ios::in | ios::binary);   
        
      if(!filein) {
          std::cout << "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] = FrameTomxArray(f);
    if( nlhs == 2 )
        plhs[1] = UShortFrameTomxArray(u);

    mxFree(f);
    mxFree(u);
    mxFree(filename);
    
}

// Convert a Frame structure to an mxArray

mxArray *FrameTomxArray(struct Frame *f)
{
    mxArray *pm, *mx;
    mwSize m, n;
    const int nfields = 3;
    const char *fieldnames[3] = {"frame_no","dist","ampl"};
    const mwIndex index = 0;
    
    m = 1;
    n = 1;
    pm = mxCreateStructMatrix(m, n, nfields, fieldnames);
    
    m = 1;
    n = 1;
    mx = mxCreateNumericMatrix(m, n, mxDOUBLE_CLASS, mxREAL);
    memcpy(mxGetPr(mx), &(f->frame_no), sizeof(double));
    mxSetFieldByNumber(pm, index, 0, mx);
    
    m = 1;
    n = ARRAYSIZE;
    mx = mxCreateNumericMatrix(m, n, mxDOUBLE_CLASS, mxREAL);
    memcpy(mxGetPr(mx), (f->dist), sizeof(double)*ARRAYSIZE);
    mxSetFieldByNumber(pm, index, 1, mx);
    
    m = 1;
    n = ARRAYSIZE;
    mx = mxCreateNumericMatrix(m, n, mxDOUBLE_CLASS, mxREAL);
    memcpy(mxGetPr(mx), (f->ampl), sizeof(double)*ARRAYSIZE);
    mxSetFieldByNumber(pm, index, 2, mx);
    
    return pm;
}

// Convert a UShortFrame structure to an mxArray

mxArray * UShortFrameTomxArray(struct UShortFrame *u)
{
    mxArray *pm, *mx;
    mwSize m, n;
    const int nfields = 3;
    const char *fieldnames[3] = {"frame_no","dist","ampl"};
    const mwIndex index = 0;
    
    m = 1;
    n = 1;
    pm = mxCreateStructMatrix(m, n, nfields, fieldnames);
    
    m = 1;
    n = 1;
    mx = mxCreateNumericMatrix(m, n, mxUINT16_CLASS, mxREAL);
    memcpy(mxGetPr(mx), &(u->frame_no), sizeof(unsigned short));
    mxSetFieldByNumber(pm, index, 0, mx);
    
    m = 1;
    n = ARRAYSIZE;
    mx = mxCreateNumericMatrix(m, n, mxUINT16_CLASS, mxREAL);
    memcpy(mxGetPr(mx), (u->dist), sizeof(unsigned short)*ARRAYSIZE);
    mxSetFieldByNumber(pm, index, 1, mx);
    
    m = 1;
    n = ARRAYSIZE;
    mx = mxCreateNumericMatrix(m, n, mxUINT16_CLASS, mxREAL);
    memcpy(mxGetPr(mx), (u->ampl), sizeof(unsigned short)*ARRAYSIZE);
    mxSetFieldByNumber(pm, index, 2, mx);
    
    return pm;
}