MATLAB Answers

uicontrol callback function with single variable

1 view (last 30 days)
Morgan Clendennin
Morgan Clendennin on 23 May 2021
Commented: Morgan Clendennin on 23 May 2021
I am trying to create a GUI that will have multiple pushbuttons which I want to reference the same callback function with the caveat being that the only piece that changes in the callback function is a single variable which I want to pass to the function in the syntax for the uicontrol. For example, lets say i have the following uicontrols
%pushbutton for 1x line bet
uicontrol('Parent',display,...
'Style','pushbutton',...
'String','1x',...
'Units','normalized',...
'Position',[0.8,0.70,0.15,0.06],...
'Callback',{@test,1});
%pushbutton for 2x line bet
uicontrol('Parent',display,...
'Style','pushbutton',...
'String','2x',...
'Units','normalized',...
'Position',[0.8,0.60,0.15,0.06],...
'Callback',{@test,2});
%pushbutton for 5x line bet
uicontrol('Parent',display,...
'Style','pushbutton',...
'String','3x',...
'Units','normalized',...
'Position',[0.8,0.50,0.15,0.06],...
'Callback',{@test,3});
and lets say this is my callback function
function test(multiple)
x = [0,1,2,3];
y = multiple .* x
end
I've tried it both the way that I have it shown in my example as well as changing the syntax for the callback function to function test(multiple, ~) and the uicontrol syntax to {@test,1,~} for example with no sucess either way. Both cases give me an error that says I have too many input arguments. Can anyone tell me what I'm doing wrong?

Answers (1)

G A
G A on 23 May 2021
Edited: G A on 23 May 2021
The following works. Though, I cannot explain why
function test(~,~,multiple)
  1 Comment
Morgan Clendennin
Morgan Clendennin on 23 May 2021
Well i can't explain it either but that works for me as well. The actual callback function that I'm working with is a little bit more complicated than the example I gave but I tried out your suggestion of adding the ~,~ before the two variables that i'm passing into the function and it worked!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by