Creating a pushbutton and callback function

148 visualizaciones (últimos 30 días)
K VdB
K VdB el 24 de Oct. de 2016
Comentada: Walter Roberson el 26 de Nov. de 2021
I could not find the right answer after a long search.
I want to create several pushbuttons in the script:
PushButton = uicontrol(gcf,'Style', 'push', 'String', 'Next', ... 'Position', [300 10 30 30], ...
'CallBack', 'PushB');
Then when I push the pushbutton I want the program to run a related function
function PushB_Callback('variables')
display('I pushed the pushbutton')
end
How do I do this?
thanks

Respuesta aceptada

Mischa Kim
Mischa Kim el 24 de Oct. de 2016
Editada: Mischa Kim el 24 de Oct. de 2016
Try
function myButtonTest()
PushButton = uicontrol(gcf,'Style', 'push', 'String', 'Next','Position', [300 10 30 30],'CallBack', @PushB);
function PushB(source,event)
display('I pushed the pushbutton')
end
end
For more information, check out the example in the documentation.
  7 comentarios
Mischa Kim
Mischa Kim el 24 de Oct. de 2016
@Simon: Absolutely. I just copy-pasted the code without replacing the quotation marks. Thanks.
K VdB
K VdB el 24 de Oct. de 2016
It works! Fantastic!

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 24 de Oct. de 2016
Editada: Jan el 26 de Oct. de 2016
Use a function handle:
function main()
PushButton = uicontrol(gcf,'Style', 'push', 'String', 'Next', ...
'Position', [300 10 30 30], ...
'CallBack', @PushB_Callback); % [EDITED, Typo: "_Callback" added]
end
function PushB_Callback(ObjectH, EventData)
display('I pushed the pushbutton')
end
  4 comentarios
Brian Njenga
Brian Njenga el 26 de Nov. de 2021
What is ObjectH in the function handle
Walter Roberson
Walter Roberson el 26 de Nov. de 2021
In MATLAB, callback functions are automatically passed at least two parameters.
The first parameter is the handle of the object for which the callback is taking place. This allows you to use the same callback function for different objects, with the function using the passed-in handle to know which object it was invoked for. So in the case of this sample code, the first parameter, here called ObjectH, will be passed as the handle of the uicontrol -- it will have the same value as PushButton has.
The second parameter is event information that describes in more detail what was requested. For most kinds of callbacks, there is no additional information and [] is passed. For some kinds of callbacks such as WindowsButtonPressFcn the parameter is a struct that has information about which button (or key) was pressed, or what the current bounds are (for constraint callbacks), and so on.
For uicontrol associated with "traditional" figures, when using Callback, then the event data will be a object of type ActionData, that has a property Source (which will be a handle to the control that was activated), and property EventName, which will be the character vector with content 'Action'

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by