用于切换另一个按钮的Matlab GUI按钮

时间:2012-11-15 17:04:19

标签: matlab user-interface button toggle matlab-guide

有没有办法在Matlab GUI中创建两个切换按钮,以便一个切换另一个?换句话说,如果按钮A打开,如何创建一个按钮B,当打开时A会关闭?

2 个答案:

答案 0 :(得分: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)

结果是我的第一个按钮切换但else之后的命令没有执行。 有没有办法执行该命令?

答案 1 :(得分:0)

我有版本R2009a,所以我不知道这是否适合你:

我用guide定义了两个按钮(默认名称)。单击第一个时,它被禁用(启用:关闭),其值设置为1,其字符串设置为开;第二个按钮设置为另一个状态。另一个按钮也有类似的行为。

% --- 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

这是你需要的吗?