Thread Subject: Garbage out of MEX function

Subject: Garbage out of MEX function

From: Petr Krysl

Date: 14 Feb, 2008 06:01:02

Message: 1 of 3

I am stumped: I can see that before I return from a MEX
routine, the output argument contains correct data. Once I
get back the output into Matlab, it is full of -1.#INF.

Why? What am I doing wrong? If you can see it, please tell me.

Thanks,

Petr

#include "mex.h"
#include "colsolconfig.h"

static int decomp_double(double *a, COLSOL_Index *maxa,
mwIndex neq);
static int decomp_float(float *a, COLSOL_Index *maxa,
mwIndex neq);

void mexFunction(
    int nargout,
    mxArray *pargout[ ],
    int nargin,
    const mxArray *pargin[ ])
{
    mwIndex neq=0;
    COLSOL_Index *MAXA=0;
    
    if (!((nargin == 2))) {
        mexErrMsgTxt ("Usage:\n" "
A=colsoldecomp(A,MAXA) ;") ;
    }
    
    
#ifdef LARGEARRAYDIMS
    if (!mxIsUint64(pargin [1])) {
        mexErrMsgTxt ("colsoldecomp: MAXA must be uint64") ;
    }
#else
    if (!mxIsUint32(pargin [1])) {
        mexErrMsgTxt ("colsoldecomp: MAXA must be uint32") ;
    }
#endif
    MAXA = (COLSOL_Index *) mxGetData (pargin [1]) ;
    if (mxIsComplex (pargin [0])) {
     mexErrMsgTxt ("colsoldecomp: A must be a real vector") ;
    }
    neq = max(mxGetM(pargin [1]),mxGetN(pargin [1]))-1;
    if (neq<=0) {
        mexErrMsgTxt ("colsoldecomp: MAXA empty, no
equations?") ;
    }

    printf ("nargout =%d\n",nargout);
    
    
    if (mxIsClass(pargin [0], "double")) {
        double *Ain= mxGetPr (pargin [0]) ;
        double *Aout= 0 ;
        int n;
        pargout [0] = mxCreateDoubleMatrix (mxGetM(pargin
[0]), mxGetN(pargin [0]), mxREAL) ;
        Aout= mxGetPr (pargout [0]) ;
//correct before modification
        for (n = 0; n < MAXA [neq]; ++n) { Aout[n] =Ain[n]; }
        for (n = 0; n < MAXA [neq]; ++n) { printf ("A [%d]
=%g\n",n,Aout[n]); }
        if (! (decomp_double(Aout, MAXA, neq)==0)) {
            mexErrMsgTxt ("colsoldecomp: A factorization
failed") ;
        }
//correct after modification
        for (n = 0; n < MAXA [neq]; ++n) { printf ("A [%d]
=%g\n",n,Aout [n]); }
   } else if (mxIsClass(pargin [0], "single")) {
     ...
    }
 //return and find garbage. Why?
}

Subject: Garbage out of MEX function

From: James Tursa

Date: 14 Feb, 2008 07:53:02

Message: 2 of 3

"Petr Krysl" <pkryslNOSP@Mucsd.edu> wrote in message
<fp0let$fnp$1@fred.mathworks.com>...
> I am stumped: I can see that before I return from a MEX
> routine, the output argument contains correct data. Once I
> get back the output into Matlab, it is full of -1.#INF.
>
> Why? What am I doing wrong? If you can see it, please
tell me.
>
> Thanks,
>
> Petr

Could you give a small example of inputs? I compiled it and
ran it with simple inputs like a=rand(3,3), b=int32(1:3),
and got some output (copy of a) with no problems. A small
input set from you would help me. Thanks.

James Tursa

Subject: Garbage out of MEX function

From: Petr Krysl

Date: 14 Feb, 2008 14:57:01

Message: 3 of 3

"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in
> Could you give a small example of inputs? I compiled it and
> ran it with simple inputs like a=rand(3,3), b=int32(1:3),
> and got some output (copy of a) with no problems. A small
> input set from you would help me. Thanks.


James,

Thank you very much. Here is the full code and a test is
also included.

http://hogwarts.ucsd.edu/~pkrysl/public/colsol.zip

My Matlab is 2007b, but I've also tried with 2008a, with the
same results.

Regards,

Petr

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com