Borrar filtros
Borrar filtros

call callback without mmouse

4 visualizaciones (últimos 30 días)
Amirhosein Ghenaati
Amirhosein Ghenaati el 6 de Nov. de 2014
Comentada: Roger el 30 de Dic. de 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

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 6 de Nov. de 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 comentarios
Amirhosein Ghenaati
Amirhosein Ghenaati el 7 de Nov. de 2014
i understood your way completely and executed your relevant comments
you solved my problem god bless you cause
thanks a lot...
Roger
Roger el 30 de Dic. de 2015
thanks ,nice try

Iniciar sesión para comentar.

Más respuestas (2)

Amirhosein Ghenaati
Amirhosein Ghenaati el 7 de Nov. de 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 comentario
Geoff Hayes
Geoff Hayes el 7 de Nov. de 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).

Iniciar sesión para comentar.


Amirhosein Ghenaati
Amirhosein Ghenaati el 8 de Nov. de 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 comentario
Amirhosein Ghenaati
Amirhosein Ghenaati el 8 de Nov. de 2014
Editada: Amirhosein Ghenaati el 8 de Nov. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by