有没有办法在 Matlab GUI 中创建两个切换按钮,以便一个切换另一个?换句话说,如果按钮 A 处于打开状态,我如何创建一个按钮 B,它在打开时会使 A 关闭?
问问题
8394 次
2 回答
1
这是我的代码:
function button1_Callback(hObject, eventdata, handles)
if get(hObject,'Value')==1
%do something here
else
%do something else (in my case delete a video object)
如果我现在放第二个按钮并使用命令
set(handles.button1,'Value',0)
The result is that my first button toggles but the command after the else
is not executed.
Is there a way to execute that command as well?
于 2012-11-15T19:28:29.200 回答
0
我有版本 R2009a,所以我不知道这是否适合您:
guide
我用(默认名称)定义了两个按钮。单击第一个时,它被禁用(启用:关闭),其值设置为 1,其字符串设置为 On;第二个按钮被设置为另一个状态。另一个按钮也有类似的行为。
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
set(handles.pushbutton1,'Value',1,'String','On','Enable','Off')
set(handles.pushbutton2,'Value',0,'String','Off','Enable','On')
get(handles.pushbutton1)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
set(handles.pushbutton1,'Value',0,'String','Off','Enable','On')
set(handles.pushbutton2,'Value',1,'String','On','Enable','Off')
如果要根据 button1 的状态切换button2 的行为,请执行以下操作:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% this toggles button1 between 0 and 1 and its label between 'On' and 'Off'
p = 1-get(handles.pushbutton1,'Value');
set(handles.pushbutton1,'Value',p)
if p==0
set(handles.pushbutton1,'String','Off')
else
set(handles.pushbutton1,'String','On')
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% Behavior of button2 is dependent on state of button1
p = get(handles.pushbutton1,'Value');
if p==0
% Do this when button1 has its label to 'Off' (and Value to 0)
else
% This will execute when button1 has a Value of 1 (and its label showing 'On')
end
这是你需要的吗?
于 2012-11-15T18:07:36.290 回答