如何使用matlab中单选按钮的回调更改布尔值的状态
How to change the status of a boolean with Callbacks from radiobuttons in matlab
我创建了一个小示例程序来演示我的问题(变量 "option1selcted" 不会更改其值):
classdef radioexample < handle
%radioexample2
% example for radiobuttuns
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% buttons
buttonTest
% radio items
radiogroup
radio1
radio2
% statis variable
option1selcted = true;
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Rradioexample','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,...
'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,...
'HandleVisibility','off');
obj.buttonTest = uicontrol('Style','pushbutton','String','Print radiostaus intop the Command Window',...
'Position',[40,200,260,60],...
'Callback',{@obj.test_Callback,obj,'test'},'Enable','On');
set(obj.formMain,'Visible','on');
fprintf('programm started');
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
function opt1_Callback(hObject, evt)
fprintf('switched to radio1 mode\n');
obj.option1selcted = true; %<- does not change the variable?
end
function opt2_Callback(hObject, evt)
fprintf('switched to radio2 mode\n');
%msgbox('switched to radio2 mode','Success');
obj.option1selcted = false; %<- does not change the variable?
end
function test_Callback(~,~,obj,val)
fprintf('the status of the radiobutton is: %d\n', obj.option1selcted);
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end
为了测试变量 "option1selcted" 的状态,测试按钮将其状态写入 matlab 命令 window。
您不能在静态方法中更改 obj
的值。在非静态方法块中定义回调函数。您需要为输入参数添加另一个 ~
。我也在按钮的回调中修复了这个问题。
完整代码如下:
classdef radioexample < handle
%radioexample2
% example for radiobuttuns
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% buttons
buttonTest
% radio items
radiogroup
radio1
radio2
% statis variable
option1selcted = true;
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Rradioexample','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,...
'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,...
'HandleVisibility','off');
obj.buttonTest = uicontrol('Style','pushbutton','String','Print radiostaus intop the Command Window',...
'Position',[40,200,260,60],...
'Callback',{@obj.test_Callback,obj,'test'},'Enable','On');
set(obj.formMain,'Visible','on');
fprintf('programm started');
end
function opt1_Callback(~,hObject,evt)
fprintf('switched to radio1 mode\n');
obj.option1selcted = true;
end
function opt2_Callback(~,hObject,evt)
fprintf('switched to radio2 mode\n');
%msgbox('switched to radio2 mode','Success');
obj.option1selcted = false;
end
function test_Callback(~,~,~,obj,val)
fprintf('the status of the radiobutton is: %d\n', obj.option1selcted);
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end
我创建了一个小示例程序来演示我的问题(变量 "option1selcted" 不会更改其值):
classdef radioexample < handle
%radioexample2
% example for radiobuttuns
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% buttons
buttonTest
% radio items
radiogroup
radio1
radio2
% statis variable
option1selcted = true;
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Rradioexample','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,...
'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,...
'HandleVisibility','off');
obj.buttonTest = uicontrol('Style','pushbutton','String','Print radiostaus intop the Command Window',...
'Position',[40,200,260,60],...
'Callback',{@obj.test_Callback,obj,'test'},'Enable','On');
set(obj.formMain,'Visible','on');
fprintf('programm started');
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
function opt1_Callback(hObject, evt)
fprintf('switched to radio1 mode\n');
obj.option1selcted = true; %<- does not change the variable?
end
function opt2_Callback(hObject, evt)
fprintf('switched to radio2 mode\n');
%msgbox('switched to radio2 mode','Success');
obj.option1selcted = false; %<- does not change the variable?
end
function test_Callback(~,~,obj,val)
fprintf('the status of the radiobutton is: %d\n', obj.option1selcted);
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end
为了测试变量 "option1selcted" 的状态,测试按钮将其状态写入 matlab 命令 window。
您不能在静态方法中更改 obj
的值。在非静态方法块中定义回调函数。您需要为输入参数添加另一个 ~
。我也在按钮的回调中修复了这个问题。
完整代码如下:
classdef radioexample < handle
%radioexample2
% example for radiobuttuns
properties(Hidden)
% all elements of the GUI are properties of the class trechner
formMain; % "The MainWindow"
menuFile; % "The Menu Header"
% buttons
buttonTest
% radio items
radiogroup
radio1
radio2
% statis variable
option1selcted = true;
end
methods(Hidden)
function obj = radioexample
% Constructor Form Main
obj.formMain = figure('position',[400,400,600,260],'Visible','off');
set(obj.formMain,'Name','Rradioexample','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,...
'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,...
'HandleVisibility','off');
obj.buttonTest = uicontrol('Style','pushbutton','String','Print radiostaus intop the Command Window',...
'Position',[40,200,260,60],...
'Callback',{@obj.test_Callback,obj,'test'},'Enable','On');
set(obj.formMain,'Visible','on');
fprintf('programm started');
end
function opt1_Callback(~,hObject,evt)
fprintf('switched to radio1 mode\n');
obj.option1selcted = true;
end
function opt2_Callback(~,hObject,evt)
fprintf('switched to radio2 mode\n');
%msgbox('switched to radio2 mode','Success');
obj.option1selcted = false;
end
function test_Callback(~,~,~,obj,val)
fprintf('the status of the radiobutton is: %d\n', obj.option1selcted);
end
end
methods(Static,Access=private)
function close_Callback(~,~,obj)
% close window
close(obj.f)
end
end
methods(Access=public,Hidden)
function disp(obj)
end
end
end