필터 지우기
필터 지우기

Help with switches in GUI

조회 수: 4 (최근 30일)
Marnie
Marnie 2015년 5월 26일
답변: Ingrid 2015년 5월 26일
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

채택된 답변

Ingrid
Ingrid 2015년 5월 26일
I think you are confused on how to add values to the guidata as now you are overwriting the values of OnOff by the pause values. Just use the debugging tool in matlab to step through your code line by line to see what is happening to your variables, especially starting from the onOffHandle
that said, you can check this with this code which should do what you want:
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 and Pause button
guiHandleData = guihandles(fig);
guiHandleData.stringsOnOff = {'On';'Off'};
guiHandleData.stringsPause = {'Pause';'Paused'};
guidata(fig, guiHandleData);
%%Function for onOff button
function varargout = onOff_Callback(a, eventdata)
% Get the current string
str = get(a,'String');
% See which string the current string matches with
ind = find(strcmp(str,guiHandleData.stringsOnOff));
% Switch to the other string
if ind == 1
set(a,'String',guiHandleData.stringsOnOff{2});
else
set(a,'String',guiHandleData.stringsOnOff{1});
end
end
%%Function for pause button
function varargout = pause_Callback(b, eventdata)
% Get the current string
st = get(b,'String');
% See which string the current string matches with
ind = find(strcmp(st,guiHandleData.stringsPause));
% Switch to the other string
if ind == 1
set(b,'String',guiHandleData.stringsPause{2});
else
set(b,'String',guiHandleData.stringsPause{1});
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by