필터 지우기
필터 지우기

uicontrol callback function with single variable

조회 수: 2 (최근 30일)
Morgan Clendennin
Morgan Clendennin 2021년 5월 23일
댓글: Morgan Clendennin 2021년 5월 23일
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