从 Matlab 中的单选按钮获取回调
Getting a callback from a radiobutton in Matlab
我有一个带有两个单选按钮的简单表单。通过更改选择,我想在回调函数中执行一些代码
这是我的示例代码,它不起作用:
classdef radioexample < handle
%radioexample2
% example for radiobuttons
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% radio items
radiogroup
radio1
radio2
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Radioexample','NumberTitle','off',...
'MenuBar','none','Resize','Off');
% a menu for exit the program
obj.menuFile.main = uimenu('Label','File');
obj.menuFile.exit = uimenu(obj.menuFile.main,...
'Label','Exit','Callback',{@obj.close_Callback,obj});
% radiobutton to select the mode
obj.radiogroup = uibuttongroup(obj.formMain,...
'Visible','on',...
'Units','pixels',...
'BackGroundColor',[0.8 0.8 0.8],...
'Position',[220 80 100 100]);
%'SelectionChangedFcn',@obj.bselection);
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 1',...
'Position',[10 70 70 20],...
'Callback',{@obj.opt1_Callback,obj},...
'HandleVisibility','off');
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 2',...
'Position',[10 50 70 20],...
'Callback',{@obj.opt2_Callback,obj},...
'HandleVisibility','off');
set(obj.formMain,'Visible','on');
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
function opt1_Callback(~)
fprintf('switched to radio1 mode'); %Does not work
end
function opt2_Callback(~)
fprintf('switched to radio2 mode'); %Does not work
msgbox('switched to radio2 mode','Success'); %Does not work
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end
回调函数没有得到executed/called。
这相对容易修复。只需在 uicontrol
行中进行以下替换:
'Callback',@obj.opt1_Callback,...
和
'Callback',@obj.opt2_Callback,...
回调函数需要两个输入参数。第一个是 uicontrol,第二个是 eventdata。如果您不需要输入参数,只需像这样添加另一个 ~
:
function opt1_Callback(~,~)
fprintf('switched to radio1 mode');
end
和
function opt2_Callback(~,~)
fprintf('switched to radio2 mode');
msgbox('switched to radio2 mode','Success');
end
另一种方法是像这样使用 uibuttongroup
的 SelectionChangedFcn
-回调:
classdef radioexample < handle
%radioexample2
% example for radiobuttons
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% radio items
radiogroup
radio1
radio2
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Radioexample','NumberTitle','off',...
'MenuBar','none','Resize','Off');
% a menu for exit the program
obj.menuFile.main = uimenu('Label','File');
obj.menuFile.exit = uimenu(obj.menuFile.main,...
'Label','Exit','Callback',{@obj.close_Callback,obj});
% radiobutton to select the mode
obj.radiogroup = uibuttongroup(obj.formMain,...
'Visible','on',...
'Units','pixels',...
'BackGroundColor',[0.8 0.8 0.8],...
'Position',[220 80 100 100],...
'SelectionChangedFcn',@obj.bselection);
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 1',...
'Position',[10 70 70 20],...
'HandleVisibility','off');
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 2',...
'Position',[10 50 70 20],...
'HandleVisibility','off');
set(obj.formMain,'Visible','on');
end
function bselection(source,~,callbackdata)
switch callbackdata.NewValue.String
case 'Option 1'
radioexample.opt1_Callback(source,callbackdata);
case 'Option 2'
radioexample.opt2_Callback(source,callbackdata);
end
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
function opt1_Callback(~,~)
fprintf('switched to radio1 mode');
end
function opt2_Callback(~,~)
fprintf('switched to radio2 mode');
msgbox('switched to radio2 mode','Success');
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end
我有一个带有两个单选按钮的简单表单。通过更改选择,我想在回调函数中执行一些代码
这是我的示例代码,它不起作用:
classdef radioexample < handle
%radioexample2
% example for radiobuttons
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% radio items
radiogroup
radio1
radio2
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Radioexample','NumberTitle','off',...
'MenuBar','none','Resize','Off');
% a menu for exit the program
obj.menuFile.main = uimenu('Label','File');
obj.menuFile.exit = uimenu(obj.menuFile.main,...
'Label','Exit','Callback',{@obj.close_Callback,obj});
% radiobutton to select the mode
obj.radiogroup = uibuttongroup(obj.formMain,...
'Visible','on',...
'Units','pixels',...
'BackGroundColor',[0.8 0.8 0.8],...
'Position',[220 80 100 100]);
%'SelectionChangedFcn',@obj.bselection);
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 1',...
'Position',[10 70 70 20],...
'Callback',{@obj.opt1_Callback,obj},...
'HandleVisibility','off');
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 2',...
'Position',[10 50 70 20],...
'Callback',{@obj.opt2_Callback,obj},...
'HandleVisibility','off');
set(obj.formMain,'Visible','on');
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
function opt1_Callback(~)
fprintf('switched to radio1 mode'); %Does not work
end
function opt2_Callback(~)
fprintf('switched to radio2 mode'); %Does not work
msgbox('switched to radio2 mode','Success'); %Does not work
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end
回调函数没有得到executed/called。
这相对容易修复。只需在 uicontrol
行中进行以下替换:
'Callback',@obj.opt1_Callback,...
和
'Callback',@obj.opt2_Callback,...
回调函数需要两个输入参数。第一个是 uicontrol,第二个是 eventdata。如果您不需要输入参数,只需像这样添加另一个 ~
:
function opt1_Callback(~,~)
fprintf('switched to radio1 mode');
end
和
function opt2_Callback(~,~)
fprintf('switched to radio2 mode');
msgbox('switched to radio2 mode','Success');
end
另一种方法是像这样使用 uibuttongroup
的 SelectionChangedFcn
-回调:
classdef radioexample < handle
%radioexample2
% example for radiobuttons
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% radio items
radiogroup
radio1
radio2
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Radioexample','NumberTitle','off',...
'MenuBar','none','Resize','Off');
% a menu for exit the program
obj.menuFile.main = uimenu('Label','File');
obj.menuFile.exit = uimenu(obj.menuFile.main,...
'Label','Exit','Callback',{@obj.close_Callback,obj});
% radiobutton to select the mode
obj.radiogroup = uibuttongroup(obj.formMain,...
'Visible','on',...
'Units','pixels',...
'BackGroundColor',[0.8 0.8 0.8],...
'Position',[220 80 100 100],...
'SelectionChangedFcn',@obj.bselection);
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 1',...
'Position',[10 70 70 20],...
'HandleVisibility','off');
uicontrol(obj.radiogroup,...
'Style',...
'radiobutton',...
'BackGroundColor',[0.8 0.8 0.8],...
'String','Option 2',...
'Position',[10 50 70 20],...
'HandleVisibility','off');
set(obj.formMain,'Visible','on');
end
function bselection(source,~,callbackdata)
switch callbackdata.NewValue.String
case 'Option 1'
radioexample.opt1_Callback(source,callbackdata);
case 'Option 2'
radioexample.opt2_Callback(source,callbackdata);
end
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
function opt1_Callback(~,~)
fprintf('switched to radio1 mode');
end
function opt2_Callback(~,~)
fprintf('switched to radio2 mode');
msgbox('switched to radio2 mode','Success');
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end