From: Kate J. on
I’m attempting to pass a single integer variable from a GUIDE-generated m-file to a C-MEX s-function c-file. I am using the following strategy:

1. In the gui m-file, I am assigning the variable, named Rating, with value UserRating, to the base workspace with the code:
Assignin(‘base’,’Rating’,UserRating);
(I have verified that this step is working fine, and have checked the value of Rating in the base workspace.)

2. Within my C-MEX s-function c-file, I use the function mexGetVariable( ) to grab the Rating value from the base workspace.

It is this step (2) that is giving me problems. So far, I can successfully compile and execute the code
const mxArray *Rating_ptr;
Rating_ptr = mexGetVariable(“base”,”Rating”);

However, when I try to view the value of Rating_ptr (by assigning it to another variable, and then viewing it in a scope within the Simulink model that calls this mex file), I am getting compiler errors when I try to include the statement
outvar = Rating_ptr or *Rating_ptr

I also tried declaring int *pr; and then using the statement pr = mxGetPr(Rating_ptr);

While these additional statements compile fine, and I’m allowed to assign outvar = *pr with no problem, the value of *pr is always 0, whereas the Rating value that I need to access is supposed to vary with user input to the gui. I have confirmed that, even when the user is inputting values to the gui & changing the value of Rating, the value of *pr is always 0.

I used the mxGetM(Rating_ptr) and mxGetN(Rating_ptr) calls to determine that the array dimensions of Rating_ptr are 1 x 1, which is consistent with the fact that I’m just passing a single variable, Rating.

For your reference, I’ve been referring to the following 2 resources for syntax help:

Mathworks documentation for mexGetVariable:
http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mexgetvariable.html

Example that uses mexGetVariable() & mxGetPr() calls:
http://newsgroups.derkeiler.com/Archive/Comp/comp.soft-sys.matlab/2006-07/msg01635.html


Does anyone know how my syntax is wrong, and how I can correctly access, within my C-MEX s-function c-code, the value of Rating that I’m reading in? I've tried every syntax combination I can think of.
Thanks in advance for your help!
From: James Tursa on
"Kate J." <quisquiliae3(a)hotmail.com> wrote in message <hlk1ea$hlc$1(a)fred.mathworks.com>...
> I&#8217;m attempting to pass a single integer variable from a GUIDE-generated m-file to a C-MEX s-function c-file. I am using the following strategy:
>
> 1. In the gui m-file, I am assigning the variable, named Rating, with value UserRating, to the base workspace with the code:
> Assignin(&#8216;base&#8217;,&#8217;Rating&#8217;,UserRating);
> (I have verified that this step is working fine, and have checked the value of Rating in the base workspace.)
>
> 2. Within my C-MEX s-function c-file, I use the function mexGetVariable( ) to grab the Rating value from the base workspace.
>
> It is this step (2) that is giving me problems. So far, I can successfully compile and execute the code
> const mxArray *Rating_ptr;
> Rating_ptr = mexGetVariable(&#8220;base&#8221;,&#8221;Rating&#8221;);
>
> However, when I try to view the value of Rating_ptr (by assigning it to another variable, and then viewing it in a scope within the Simulink model that calls this mex file), I am getting compiler errors when I try to include the statement
> outvar = Rating_ptr or *Rating_ptr
>
> I also tried declaring int *pr; and then using the statement pr = mxGetPr(Rating_ptr);
>
> While these additional statements compile fine, and I&#8217;m allowed to assign outvar = *pr with no problem, the value of *pr is always 0, whereas the Rating value that I need to access is supposed to vary with user input to the gui. I have confirmed that, even when the user is inputting values to the gui & changing the value of Rating, the value of *pr is always 0.
>
> I used the mxGetM(Rating_ptr) and mxGetN(Rating_ptr) calls to determine that the array dimensions of Rating_ptr are 1 x 1, which is consistent with the fact that I&#8217;m just passing a single variable, Rating.
>
> For your reference, I&#8217;ve been referring to the following 2 resources for syntax help:
>
> Mathworks documentation for mexGetVariable:
> http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mexgetvariable.html
>
> Example that uses mexGetVariable() & mxGetPr() calls:
> http://newsgroups.derkeiler.com/Archive/Comp/comp.soft-sys.matlab/2006-07/msg01635.html
>
>
> Does anyone know how my syntax is wrong, and how I can correctly access, within my C-MEX s-function c-code, the value of Rating that I&#8217;m reading in? I've tried every syntax combination I can think of.
> Thanks in advance for your help!

Need to see more of your code. What is the class of the variable Rating? Is it a double, single, char, int32, etc.? What is the type of the variable outvar? etc. etc. etc.

Basically, mexGetVariable gets a copy of the workspace variable into your mex function. Using mxGetPr and then dereferencing should show the value as long as the class of the mxArray is double.

James Tursa