필터 지우기
필터 지우기

call callback without mmouse

조회 수: 4 (최근 30일)
Amirhosein Ghenaati
Amirhosein Ghenaati 2014년 11월 6일
댓글: Roger 2015년 12월 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
Amirhosein Ghenaati 2014년 11월 7일
i understood your way completely and executed your relevant comments
you solved my problem god bless you cause
thanks a lot...
Roger
Roger 2015년 12월 30일
thanks ,nice try

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Amirhosein Ghenaati
Amirhosein Ghenaati 2014년 11월 7일
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
Amirhosein Ghenaati 2014년 11월 8일
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일
편집: 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