Effacer les filtres
Effacer les filtres

Help with switches in GUI

4 vues (au cours des 30 derniers jours)
Marnie
Marnie le 26 Mai 2015
Réponse apportée : Ingrid le 26 Mai 2015
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

Réponse acceptée

Ingrid
Ingrid le 26 Mai 2015
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

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by