Path: news.mathworks.com!not-for-mail
From: "Faisal " <faisal_mufti.nospam@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Pass structure to MATLAB
Date: Fri, 2 Nov 2007 06:10:21 +0000 (UTC)
Organization: Australian National University
Lines: 184
Message-ID: <fgef0d$pqs$1@fred.mathworks.com>
References: <fgb6fp$8rt$1@fred.mathworks.com> <6p9li3t0t4mc554emmf7q74rd5iut85d5l@4ax.com>
Reply-To: "Faisal " <faisal_mufti.nospam@yahoo.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1193983821 26460 172.30.248.38 (2 Nov 2007 06:10:21 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 2 Nov 2007 06:10:21 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1014052
Xref: news.mathworks.com comp.soft-sys.matlab:435765


Hi James,
Thanks for putting so much effort in for the reply. However,
you seems to have made all the processing within the
mexFunction. On the otherhand I want it to be used as
wrapper function. I want to do the implementation in my C++
function and have mexFunction return it.  I have posted my
code, can you please correct me how to pass the values that
my function is reading.
Thanks,

#include "mex.h"
#include "matrix.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>
 
#define ARRAYSIZE 3072
 
#ifndef mwSize
#define mwSize int
#endif
 
#ifndef mwIndex
#define mwIndex int
#endif
using namespace std;
struct Frame
{
     double frame_no;
     double dist[ARRAYSIZE];
     double ampl[ARRAYSIZE];
};
 
struct UShortFrame
{
     unsigned frame_no;
     unsigned short dist[ARRAYSIZE];
     unsigned short ampl[ARRAYSIZE];
};
void ReadFromFile(const char *filename, int ArraySize)
{
      int i;unsigned read_frame_no;
      UShortFrame* usframe = new UShortFrame;
      filename = "data.dat";

      ifstream filein(filename, ios::in | ios::binary);
      if(!filein) {
            std::cout << "Cannot open file to read.\n"; }
           if (filein.read((char*)usframe,sizeof(UShortFrame)))
             {
               Frame* frame = new Frame;
               frame->frame_no = usframe->frame_no;
                for (int i = 0; i < 3072; ++i)
               {
                   frame->dist[i] = usframe->dist[i];
                   frame->ampl[i] = usframe->ampl[i];

               }
                  
             }
            filein.close();
            delete []usframe;
            
 }
 
int mPut1(struct Frame *f, char name[]);
int mPut2(struct UShortFrame *u, char name[]);
 
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
     const mxArray *prhs[])
{
    struct Frame *f;
    struct UShortFrame *u;
    size_t t1 = 1;
    int i;
    
    f = (struct Frame *) mxCalloc( t1, sizeof(struct Frame) );
    f->frame_no = 1.0;
    for( i=0; i<ARRAYSIZE; i++ )
    {
        f->dist[i] = (double) i;
        f->ampl[i] = (double) 2*i;
    }
    
    u = (struct UShortFrame *) mxCalloc( t1, sizeof(struct
UShortFrame) );
    u->frame_no = 1;
    for( i=0; i<ARRAYSIZE; i++ )
    {
        u->dist[i] = i;
        u->ampl[i] = 2*i;
    }
    
    if( mPut1(f, "f") )
        mexPrintf("mPut1 for f did not work\n");
    
    if( mPut2(u, "u") )
        mexPrintf("mPut2 for u did not work\n");
    
    mxFree(f);
    mxFree(u);
}
 
// Put a Frame structure into MATLAB base workspace
 
int mPut1(struct Frame *f, char name[])
{
    mxArray *pm, *mx;
    mwSize m, n;
    const int nfields = 3;
    const char *fieldnames[3] = {"frame_no","dist","ampl"};
    const mwIndex index = 0;
    int r;
    
    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);
    
    r = mexPutVariable("base", name, pm);
    mxDestroyArray(pm);
    
    return r;
}
 
// Put a UShortFrame structure into MATLAB base workspace
 
int mPut2(struct UShortFrame *u, char name[])
{
    mxArray *pm, *mx;
    mwSize m, n;
    const int nfields = 3;
    const char *fieldnames[3] = {"frame_no","dist","ampl"};
    const mwIndex index = 0;
    int r;
    
    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);
    
    r = mexPutVariable("base", name, pm);
    mxDestroyArray(pm);
    
    return r;
}