Effacer les filtres
Effacer les filtres

Writing callback for a pushbutton

2 vues (au cours des 30 derniers jours)
Zsolt Lord
Zsolt Lord le 24 Mai 2013
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.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 24 Mai 2013
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.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by