Writing callback for a pushbutton

2 次查看(过去 30 天)
So I'm trying to calculate a sum to a custom N, which a user writes into a uicontrol/edit field, but I can't seem to get the callback right.
This is how I create the edit field:
handles.M1_8.up = uicontrol ('Style','edit',...
'String','n',...
'FontSize',10,...
'Position',[215 415 50 20]);
And this is the pushbutton:
handles.M1_8.calculate = uicontrol ('Style','pushbutton',...
'String','Calculate',...
'Position',[320 305 60 25],...
'Callback',@(~,~,handles) (calculate8_Callback(handles)));
And here's my function it's supposed to execute when pressing the Calculate button:
function calculate8_Callback(handles)
val = str2num(get(handles.M1_7.up,'String'));
Now, when I click the button, it says "Not enough input arguments." to the line where I wrote the callback to the pushbutton.
Any ideas why it's not working.

采纳的回答

Sean de Wolski
Sean de Wolski 2013-5-24
The issue is in how you created the callback:
handles.M1_8.calculate = uicontrol ('Style','pushbutton',...
'String','Calculate',...
'Position',[320 305 60 25],...
'Callback',@(~,~,handles) (calculate8_Callback(handles)));
The callback right now expects three inputs, ~,~,handles. However, is only gets two, the source and the eventdata. This should work as the calback:
@(~,~) (calculate8_Callback(handles)));
During the creation of the anonymous function, a snapshot of handles will be taken and then passed into calculate8_Callback.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by