<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170947</link>
    <title>MATLAB Central Newsreader - Using mexGetVariablePtr and mexGetVariable</title>
    <description>Feed for thread: Using mexGetVariablePtr and mexGetVariable</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 14 Jun 2008 18:32:01 -0400</pubDate>
      <title>Using mexGetVariablePtr and mexGetVariable</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170947#437443</link>
      <author>lee </author>
      <description>hello, I have some very simple code to get a variable from&lt;br&gt;
the matlab workspace into my mex function via&lt;br&gt;
mexGetVariablePtr. Problem is that it seems to be getting a&lt;br&gt;
0 all the time even though the workspace value (hullo=5 in&lt;br&gt;
the matlab workspace) is not zero. To debug it I'm&lt;br&gt;
printf-ing the initial value returned from&lt;br&gt;
mexGetVariablePtr, and the numbers I'm deriving from that in&lt;br&gt;
trying to get the 'hullo' value.&lt;br&gt;
&lt;br&gt;
When I delete the hullo variable fromthe matlab workspace&lt;br&gt;
and run the mex file matlab has a 'segmentation violation&lt;br&gt;
detection'. This also happens if I change the variable name&lt;br&gt;
from 'hullo' in my mex file to a nonexisting variable name.&lt;br&gt;
-So- it seems as if the mex file is getting something&lt;br&gt;
related to the 'hullo' variable, but I can't get it's actual&lt;br&gt;
value,... here are some exampled printf results from when&lt;br&gt;
I've run the program (hullo=5 in matlab):&lt;br&gt;
myarray  658681640  | alpha  20379184  |  ho1  0&lt;br&gt;
myarray  658690856  | alpha  20379264  |  ho1  0&lt;br&gt;
myarray  658601072  | alpha  20361152  |  ho1  0&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
here's the code:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray *plhs,&lt;br&gt;
int nrhs, mxArray *prhs[])&lt;br&gt;
{&lt;br&gt;
int ThreadNr;&lt;br&gt;
double *alpha;&lt;br&gt;
double ho1;&lt;br&gt;
const mxArray *myarray;&lt;br&gt;
&lt;br&gt;
myarray = mexGetVariablePtr(&quot;base&quot;, &quot;hullo&quot;);	//can be base&lt;br&gt;
caller or global&lt;br&gt;
&lt;br&gt;
alpha = mxGetPr(myarray);&lt;br&gt;
ho1 = *alpha;&lt;br&gt;
&lt;br&gt;
mexPrintf(&quot;myarray  %d  | alpha  %d  |  ho1  %d&quot;, myarray,&lt;br&gt;
alpha, ho1);&lt;br&gt;
}</description>
    </item>
    <item>
      <pubDate>Sat, 14 Jun 2008 19:58:01 -0400</pubDate>
      <title>Re: Using mexGetVariablePtr and mexGetVariable</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170947#437455</link>
      <author>Sebastiaan </author>
      <description>&amp;gt; &lt;br&gt;
&amp;gt; #include &quot;mex.h&quot;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; void mexFunction(int nlhs, mxArray *plhs,&lt;br&gt;
&amp;gt; int nrhs, mxArray *prhs[])&lt;br&gt;
First of all, which Matlab version are you using? The&lt;br&gt;
correct function call is:&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const&lt;br&gt;
mxArray* prhs[])&lt;br&gt;
&lt;br&gt;
(at least, for a number of versions).&lt;br&gt;
&amp;gt; mexPrintf(&quot;myarray  %d  | alpha  %d  |  ho1  %d&quot;, myarray,&lt;br&gt;
&amp;gt; alpha, ho1);&lt;br&gt;
&lt;br&gt;
The bug is here. The variable is recorded correctly, but it&lt;br&gt;
is a double, while printed as a integer. Use %f instead.&lt;br&gt;
&lt;br&gt;
Btw: to prevent your code from crashing, check if&lt;br&gt;
myarray!=0, e.g.:&lt;br&gt;
&lt;br&gt;
if (myarray==NULL)&lt;br&gt;
&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;Variable hullo not initialized\n&quot;)&lt;br&gt;
&lt;br&gt;
Greetings,&lt;br&gt;
Sebastiaan</description>
    </item>
    <item>
      <pubDate>Sat, 14 Jun 2008 23:12:01 -0400</pubDate>
      <title>Re: Using mexGetVariablePtr and mexGetVariable</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/170947#437474</link>
      <author>lee </author>
      <description>&quot;Sebastiaan &quot; &amp;lt;s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl&amp;gt;&lt;br&gt;
wrote in message &amp;lt;g317s9$3sm$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; #include &quot;mex.h&quot;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; void mexFunction(int nlhs, mxArray *plhs,&lt;br&gt;
&amp;gt; &amp;gt; int nrhs, mxArray *prhs[])&lt;br&gt;
&amp;gt; First of all, which Matlab version are you using? The&lt;br&gt;
&amp;gt; correct function call is:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const&lt;br&gt;
&amp;gt; mxArray* prhs[])&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (at least, for a number of versions).&lt;br&gt;
&amp;gt; &amp;gt; mexPrintf(&quot;myarray  %d  | alpha  %d  |  ho1  %d&quot;, myarray,&lt;br&gt;
&amp;gt; &amp;gt; alpha, ho1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The bug is here. The variable is recorded correctly, but it&lt;br&gt;
&amp;gt; is a double, while printed as a integer. Use %f instead.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Btw: to prevent your code from crashing, check if&lt;br&gt;
&amp;gt; myarray!=0, e.g.:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if (myarray==NULL)&lt;br&gt;
&amp;gt;   mexErrMsgTxt(&quot;Variable hullo not initialized\n&quot;)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Greetings,&lt;br&gt;
&amp;gt; Sebastiaan&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
thanks, that was it, I just posted some instructions on mex&lt;br&gt;
file multithreading (I needed the above help for a&lt;br&gt;
multithreading prog I was writing) here:&lt;br&gt;
&lt;a href=&quot;http://www.instructables.com/id/Matlab-Multithreading-EASY/&quot;&gt;http://www.instructables.com/id/Matlab-Multithreading-EASY/&lt;/a&gt;</description>
    </item>
  </channel>
</rss>

