call callback without mmouse
2 次查看(过去 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
0 个评论
采纳的回答
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
2014-11-7
1 个评论
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!