Thread Subject: Check button state from Mex File

Subject: Check button state from Mex File

From: Stiphu

Date: 16 Oct, 2009 06:34:19

Message: 1 of 4

Hi there

I want to check the state of a button in a Matlab GUI dynamically from a c++ function. The c++ function looks like that:

// Definitions
mxArray *plhs[1];
mxArray *parameter = mxCreateString("Value");
prhs[0] = hButton; // Handle to the button
prhs[1] = parameter; // property object

bool running = true;

while(running ){
   ...
   mexCallMATLAB(1,plhs,2,prhs,"get");
   running = (bool)mxGetLogicals(plhs[0]);
}

hButton is the handle to a toggle button. The problem is: I can toggle the button, but the value I read in the mex function is always "true". Looks like a kind of a master/slave problem. I guess the GUI value is not actuall, as long as the c++ function is running. Is that right? Is there a possibility to interface to the c++ function while it's running?

Subject: Check button state from Mex File

From: Jan Simon

Date: 16 Oct, 2009 08:24:03

Message: 2 of 4

Dear Stiphu!

> I want to check the state of a button in a Matlab GUI dynamically from a c++ function. The c++ function looks like that:
>
> // Definitions
> mxArray *plhs[1];
> mxArray *parameter = mxCreateString("Value");
> prhs[0] = hButton; // Handle to the button
> prhs[1] = parameter; // property object
>
> bool running = true;
>
> while(running ){
> ...
> mexCallMATLAB(1,plhs,2,prhs,"get");
> running = (bool)mxGetLogicals(plhs[0]);
> }
>

Better use mexGet/mexSet:
  const mxArray mexGet(double handle, const char * Property)
There is an example shipped with Matlab: mexget.c
And after mexSet you need a DRAWNOW. I do not know a way to trigger this from the MEX directly, so mexCallMATLAB seems to be the only way to update the handle graphic objects.

Good luck, Jan

Subject: Check button state from Mex File

From: Stiphu

Date: 19 Oct, 2009 09:29:02

Message: 3 of 4

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hb9aj2$mg0$1@fred.mathworks.com>...
> Dear Stiphu!
>
> > I want to check the state of a button in a Matlab GUI dynamically from a c++ function. The c++ function looks like that:
> >
> > // Definitions
> > mxArray *plhs[1];
> > mxArray *parameter = mxCreateString("Value");
> > prhs[0] = hButton; // Handle to the button
> > prhs[1] = parameter; // property object
> >
> > bool running = true;
> >
> > while(running ){
> > ...
> > mexCallMATLAB(1,plhs,2,prhs,"get");
> > running = (bool)mxGetLogicals(plhs[0]);
> > }
> >
>
> Better use mexGet/mexSet:
> const mxArray mexGet(double handle, const char * Property)
> There is an example shipped with Matlab: mexget.c
> And after mexSet you need a DRAWNOW. I do not know a way to trigger this from the MEX directly, so mexCallMATLAB seems to be the only way to update the handle graphic objects.
>
> Good luck, Jan

Hi Jan

That works fine now, no matter if I'm calling it with mexGet or with mexCallMatlab. Now I have a new problem. If I'm reading the dynamic state of "UserData" from a pushbutton which I toggle between 1 and 0 every time I push the button, for example the state "UserData" is (if loaded in the mexfile) not actual till the callback of the pushbutton is finished (then it's going to be refreshed). Thats a big problem if you want to toggle this state and then doing something before you leave the callback or function. Any idea how to solve this problem?

Subject: Check button state from Mex File

From: Jan Simon

Date: 19 Oct, 2009 10:18:01

Message: 4 of 4

Dear Stiphu!

> That works fine now, no matter if I'm calling it with mexGet or with mexCallMatlab. Now I have a new problem. If I'm reading the dynamic state of "UserData" from a pushbutton which I toggle between 1 and 0 every time I push the button, for example the state "UserData" is (if loaded in the mexfile) not actual till the callback of the pushbutton is finished (then it's going to be refreshed). Thats a big problem if you want to toggle this state and then doing something before you leave the callback or function. Any idea how to solve this problem?

Please start a new thread for a new problem.

The uicontrol's BusyAction and Interruptible properties can be used to manage the behaviour during the callback executes.

Kind regards, Jan

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