Help with switches in GUI
Show older comments
I'm trying to create a GUI that has an on-off switch and a Pause button, however I am new to GUI and whenever I press the button I have the same callback displayed on both.... can anyone let me know what I need to change to make this work correctly??
Thanks
function Test_Button(hObject, eventdata, onOffHandle, varargin)
%Create the GUI
fig = figure;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create a pushbutton for On/Off Button
% Initial String is 'Off'
onOff = uicontrol(fig,'Style','pushbutton',...
'String','Off',...
'Position',[460 330 70 50],...
'Callback',@onOff_Callback);
% Create a pushbutton for Pause button
% Initial String is 'Pause'
pause = uicontrol(fig,'Style','pushbutton',...
'String','Pause',...
'Position',[460 280 70 50],...
'Callback',@pause_Callback);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create the handles for onOff button
onOffHandle = guihandles(fig);
onOffHandle.strings = {'On';'Off'};
guidata(fig, onOffHandle);
% Create the handles for Pause button
pauseHandle = guihandles(fig);
pauseHandle.strings = {'Pause';'Paused'};
guidata(fig, pauseHandle);
%%Function for onOff button
function varargout = onOff_Callback(a, eventdata)
onOffHandle = guidata(a);
% Get the current string
str = get(a,'String');
% See which string the current string matches with
ind = find(strcmp(str,onOffHandle.strings));
% Switch to the other string
if ind == 1
set(a,'String',onOffHandle.strings{2});
else
set(a,'String',onOffHandle.strings{1});
end
%%Function for pause button
function varargout = pause_Callback(b, eventdata)
pauseHandle = guidata(b);
% Get the current string
st = get(b,'String');
% See which string the current string matches with
ind = find(strcmp(st,pauseHandle.strings));
% Switch to the other string
if ind == 1
set(b,'String',pauseHandle.strings{2});
else
set(b,'String',pauseHandle.strings{1});
end
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!