Creating a pushbutton and callback function

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

 채택된 답변

Mischa Kim
Mischa Kim 2016년 10월 24일
편집: Mischa Kim 2016년 10월 24일

0 개 추천

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

K VdB
K VdB 2016년 10월 24일
Thank you for your response.
I've tried the suggested syntax but this gives an error:
"Undefined function or variable 'PushB'."
-
Greetings
Mischa Kim
Mischa Kim 2016년 10월 24일
Copy-paste the entire code and save the function as myButtonTest.m.
K VdB
K VdB 2016년 10월 24일
It will still give the same error.
-
Thanks for the response
Mischa Kim
Mischa Kim 2016년 10월 24일
Just to make sure, there is only one MATLAB file, named myButtonTest.m. The PushB function is nested within myButtonTest.
Jan
Jan 2016년 10월 24일
@Mischa: And because PushB is nested, evaluating it as a string will not work. Callbacks defined as a string are evaluated in the base workspace. So better use a function handle.
Mischa Kim
Mischa Kim 2016년 10월 24일
@Simon: Absolutely. I just copy-pasted the code without replacing the quotation marks. Thanks.
K VdB
K VdB 2016년 10월 24일
It works! Fantastic!

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

추가 답변 (1개)

Jan
Jan 2016년 10월 24일
편집: Jan 2016년 10월 26일

1 개 추천

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

K VdB
K VdB 2016년 10월 24일
Then I get the following error:
Undefined function 'PushB' for input arguments of type 'matlab.ui.control.UIControl'.
_
Greetings
Jan
Jan 2016년 10월 26일
@K Vdb: This is a type. Do you see, that I've written "@PushB" but called the function "PushB_Callback"? Come on, try to follow the intention of the code.
What is ObjectH in the function handle
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'

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2016년 10월 24일

댓글:

2021년 11월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by