uicontrol callback function with single variable

4 次查看(过去 30 天)
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?

回答(1 个)

G A
G A 2021-5-23
编辑:G A 2021-5-23
The following works. Though, I cannot explain why
function test(~,~,multiple)
  1 个评论
Morgan Clendennin
Morgan Clendennin 2021-5-23
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!

请先登录,再进行评论。

类别

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

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by