Writing callback for a pushbutton

4 ビュー (過去 30 日間)
Zsolt Lord
Zsolt Lord 2013 年 5 月 24 日
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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by