필터 지우기
필터 지우기

Writing callback for a pushbutton

조회 수: 2 (최근 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 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