call callback without mmouse

1 view (last 30 days)
Amirhosein Ghenaati
Amirhosein Ghenaati on 6 Nov 2014
Commented: Roger on 30 Dec 2015
how can I excite callback methods of my pushbutton without use of mouse and only with programing command so that main GUI file be opened one time like mouse operation

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Nov 2014
Amirhosein - if you are trying to simulate the pressing of the pushbutton on the GUI from outside of the GUI (say from another script or from within the Command Window), you could do something like the following.
Assuming that you are using GUIDE, change the HandleVisibility property of your figure to on. This allows the GUI figure to be visible, so it can be "found" when we use findobj. Suppose that the Tag property of the figure is figMyGui. (Both of these properties can be set using the Property Inspector.) Suppose your GUI has a single pushbutton with the following callback
function pushbutton1_Callback(hObject, eventdata, handles)
fprintf('The push button has been pressed!\n');
So if you were to run the GUI (for example, named PushButtonTest) and press the button, a message will be written to the Command Window. Look closely at the above function - since we want to call it from elsewhere, we have to supply it with the appropriate inputs. hObject is the handle to the pushbutton1, eventdata is typically an empty matrix, and handles is the structure of all handles to the GUI controls and any user-defined data.
In the Command Window, we do the following. We need to find the figure so that we can get a hold of the handles structure. To do that we do
hGuiFig = findobj('Tag','figMyGui','Type','figure')
We are looking for the figure whose tag is figMyGui. If hGuiFig is non-empty, then we can proceed
if ~isempty(hGuiFig)
% get the handles
handles = guidata(hGuiFig);
% call the pushbutton1 callback using the name of the GUI
PushButtonTest('pushbutton1_Callback',handles.pushbutton1,[],handles);
end
If you run the above code, you should see the pushbutton1 message written to the console. Note how we invoke the callback
PushButtonTest('pushbutton1_Callback',handles.pushbutton1,[],handles);
PushButtonTest is the name of our GUI. The first input is the name of our callback function, the second is the handle to the pushbutton1 control, the third is an empty matrix, and the fourth is our handles struct.
Try the above and see what happens. I've attached an example GUI that does the above.
  2 Comments
Amirhosein Ghenaati
Amirhosein Ghenaati on 7 Nov 2014
i understood your way completely and executed your relevant comments
you solved my problem god bless you cause
thanks a lot...
Roger
Roger on 30 Dec 2015
thanks ,nice try

Sign in to comment.

More Answers (2)

Amirhosein Ghenaati
Amirhosein Ghenaati on 7 Nov 2014
I found new problem
I can not open GUI file as simply as :
PushButtonTest.m in my editor
how can I execute GUI program in my editor?
  1 Comment
Geoff Hayes
Geoff Hayes on 7 Nov 2014
Just press the green Run button that is on the Editor tab. If that isn't working, then perhaps it is due to an error (check the Command Window for any error messages).

Sign in to comment.


Amirhosein Ghenaati
Amirhosein Ghenaati on 8 Nov 2014
no i dont want to press run button with my mouse or F5 on the gui file I want to execute GUI file from another script
indeed i can run for example untitled.m file with F5(run) then GUI file must be opened automatically from untitled.m
thus i wrote in the untitled.m
clear;clc
PushButtonTest.m;PushButtonTest.m;
but it didn't work
  1 Comment
Amirhosein Ghenaati
Amirhosein Ghenaati on 8 Nov 2014
Edited: Amirhosein Ghenaati on 8 Nov 2014
yes i found my mistakes i forgot to open gui file automatically first the solution is very simple as below:
open('temp');
temp
now gui file is opened and executed automatically in untitled.m file

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!