call callback without mmouse

1 次查看(过去 30 天)
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

采纳的回答

Geoff Hayes
Geoff Hayes 2014-11-6
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 个评论
Amirhosein Ghenaati
i understood your way completely and executed your relevant comments
you solved my problem god bless you cause
thanks a lot...

请先登录,再进行评论。

更多回答(2 个)

Amirhosein Ghenaati
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 个评论
Geoff Hayes
Geoff Hayes 2014-11-7
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).

请先登录,再进行评论。


Amirhosein Ghenaati
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 个评论
Amirhosein Ghenaati
编辑:Amirhosein Ghenaati 2014-11-8
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by