Thread Subject: Using mexGetVariablePtr and mexGetVariable

Subject: Using mexGetVariablePtr and mexGetVariable

From: lee

Date: 14 Jun, 2008 18:32:01

Message: 1 of 3

hello, I have some very simple code to get a variable from
the matlab workspace into my mex function via
mexGetVariablePtr. Problem is that it seems to be getting a
0 all the time even though the workspace value (hullo=5 in
the matlab workspace) is not zero. To debug it I'm
printf-ing the initial value returned from
mexGetVariablePtr, and the numbers I'm deriving from that in
trying to get the 'hullo' value.

When I delete the hullo variable fromthe matlab workspace
and run the mex file matlab has a 'segmentation violation
detection'. This also happens if I change the variable name
from 'hullo' in my mex file to a nonexisting variable name.
-So- it seems as if the mex file is getting something
related to the 'hullo' variable, but I can't get it's actual
value,... here are some exampled printf results from when
I've run the program (hullo=5 in matlab):
myarray 658681640 | alpha 20379184 | ho1 0
myarray 658690856 | alpha 20379264 | ho1 0
myarray 658601072 | alpha 20361152 | ho1 0


here's the code:



#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs,
int nrhs, mxArray *prhs[])
{
int ThreadNr;
double *alpha;
double ho1;
const mxArray *myarray;

myarray = mexGetVariablePtr("base", "hullo"); //can be base
caller or global

alpha = mxGetPr(myarray);
ho1 = *alpha;

mexPrintf("myarray %d | alpha %d | ho1 %d", myarray,
alpha, ho1);
}

Subject: Using mexGetVariablePtr and mexGetVariable

From: Sebastiaan

Date: 14 Jun, 2008 19:58:01

Message: 2 of 3

>
> #include "mex.h"
>
> void mexFunction(int nlhs, mxArray *plhs,
> int nrhs, mxArray *prhs[])
First of all, which Matlab version are you using? The
correct function call is:

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const
mxArray* prhs[])

(at least, for a number of versions).
> mexPrintf("myarray %d | alpha %d | ho1 %d", myarray,
> alpha, ho1);

The bug is here. The variable is recorded correctly, but it
is a double, while printed as a integer. Use %f instead.

Btw: to prevent your code from crashing, check if
myarray!=0, e.g.:

if (myarray==NULL)
  mexErrMsgTxt("Variable hullo not initialized\n")

Greetings,
Sebastiaan

Subject: Using mexGetVariablePtr and mexGetVariable

From: lee

Date: 14 Jun, 2008 23:12:01

Message: 3 of 3

"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl>
wrote in message <g317s9$3sm$1@fred.mathworks.com>...
> >
> > #include "mex.h"
> >
> > void mexFunction(int nlhs, mxArray *plhs,
> > int nrhs, mxArray *prhs[])
> First of all, which Matlab version are you using? The
> correct function call is:
>
> void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const
> mxArray* prhs[])
>
> (at least, for a number of versions).
> > mexPrintf("myarray %d | alpha %d | ho1 %d", myarray,
> > alpha, ho1);
>
> The bug is here. The variable is recorded correctly, but it
> is a double, while printed as a integer. Use %f instead.
>
> Btw: to prevent your code from crashing, check if
> myarray!=0, e.g.:
>
> if (myarray==NULL)
> mexErrMsgTxt("Variable hullo not initialized\n")
>
> Greetings,
> Sebastiaan
>




thanks, that was it, I just posted some instructions on mex
file multithreading (I needed the above help for a
multithreading prog I was writing) here:
http://www.instructables.com/id/Matlab-Multithreading-EASY/

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
mex mexgetvariableptr mexgetvariable lee 14 Jun, 2008 14:35:05
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com