Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: help regarding 2D matrix using c mex file
Date: Mon, 17 Aug 2009 07:59:02 +0000 (UTC)
Organization: Indian Institute of Technology
Lines: 65
Message-ID: <h6b2k6$oh2$1@fred.mathworks.com>
Reply-To: <HIDDEN>
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 1250495942 25122 172.30.248.38 (17 Aug 2009 07:59:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 17 Aug 2009 07:59:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 223861
Xref: news.mathworks.com comp.soft-sys.matlab:563866


Hi
I am beginner and learning mex programming in C. 
I am trying to read a 2D matrix and print the pythagoras squares.
Thought compiling is successful, MATALB has thrown exception.
Can someone help me please...

thanking you very much,

with regards,
ramana 

*********
#include "mex.h"
#include <math.h>

#define OUT        plhs[0]
#define SAMPLE double   /* define the type used for data samples */

/****************************************************************************/
/* Function Declarations                                                    */
/****************************************************************************/
SAMPLE **get_matrix_pointers(int xDim, int yDim, SAMPLE *output);
void release_matrix_pointers(SAMPLE **a);
void py_dist(SAMPLE **a, int xDim, int yDim);

/****************************************************************************/
/* Gateway function and error checking                                      */
/****************************************************************************/
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
   SAMPLE *output, **a;
   int xDim, yDim;
   
   xDim = (int) mxGetM(prhs[0]);
   yDim = (int) mxGetN(prhs[0]);
   /*do the matlab stuff*/
   mexPrintf("x Dimensions = %d.\n",xDim);
   mexPrintf("y Dimensions = %d.\n",yDim);
   OUT = mxCreateDoubleMatrix(yDim, xDim, mxREAL); // mxArray is transpose of c matrix
   output = mxGetPr(OUT);
   a = mxGetPr(prhs[0]);  
   
   py_dist(a, xDim, yDim);
   release_matrix_pointers(a);
}

/****************************************************************************/
/* Free the space when we're done (just the array pointers)                 */
/****************************************************************************/
void release_matrix_pointers(SAMPLE **a)
{
    mxFree(a+1);
} 

/****************************************************************************/
/* Fill with pythagorian distances with the nice offset pointers            */
/****************************************************************************/
void py_dist(SAMPLE **a, int xDim, int yDim)
{
    int x,y;
    for(x = 1; x<=xDim; ++x)
        for (y = 1; y<=yDim; ++y)
            a[x][y] = sqrt((x*x)+(y*y));
}
************