Simple GUI programming for a counter
Dear All,
I’m trying to create a simple GUI for a counter.
The GUI consists of a plus and a minus pushbutton as well as an edit textbox for displaying the current value of the counter which starts at 1. Unfortunately, the value doesn’t apper in the display textbox. Below is my code, what could be wrong here?
Thanks,
Ben
function counter()
hfig = figure();
guidata(hfig, struct(‘counter’,1)); %set counter default value to 1
plusPushButton = uicontrol(‘Parent’, hfig,’Style’,’pushbutton’,…
‘Units’,’normalized’,…
‘Position’,[0.1 0.5 0.2 0.1],…
‘String’,’+’,…
‘Callback’,@plusPushButton_callback);
minusPushButton = uicontrol(‘Parent’, hfig,’Style’,’pushbutton’,…
‘Units’,’normalized’,…
‘Position’,[0.4 0.5 0.2 0.1],…
‘String’,’-‘,…
‘Callback’,@minusPushButton_callback);
valueBox = uicontrol(‘Parent’, hfig,’Style’,’edit’,…
‘Units’,’normalized’,…
‘Position’,[0.25 0.3 0.2 0.1],…
‘String’,’ ‘,…
‘Callback’,@valueBox_callback);
end
function plusPushButton_callback(hObject,eventdata)
handles=guidata(hObject);
handles.counter = handles.counter + 1;
guidata(hObject,handles);
end
function minusPushButton_callback(hObject,eventdata)
handles=guidata(hObject);
handles.counter = handles.counter – 1;
guidata(hObject,handles);
end
function valueBox_callback(hObject,eventdata)
handles=guidata(hObject);
set(handles.resultBox,’String’,num2str(counter)); %display current value of counter
endDear All,
I’m trying to create a simple GUI for a counter.
The GUI consists of a plus and a minus pushbutton as well as an edit textbox for displaying the current value of the counter which starts at 1. Unfortunately, the value doesn’t apper in the display textbox. Below is my code, what could be wrong here?
Thanks,
Ben
function counter()
hfig = figure();
guidata(hfig, struct(‘counter’,1)); %set counter default value to 1
plusPushButton = uicontrol(‘Parent’, hfig,’Style’,’pushbutton’,…
‘Units’,’normalized’,…
‘Position’,[0.1 0.5 0.2 0.1],…
‘String’,’+’,…
‘Callback’,@plusPushButton_callback);
minusPushButton = uicontrol(‘Parent’, hfig,’Style’,’pushbutton’,…
‘Units’,’normalized’,…
‘Position’,[0.4 0.5 0.2 0.1],…
‘String’,’-‘,…
‘Callback’,@minusPushButton_callback);
valueBox = uicontrol(‘Parent’, hfig,’Style’,’edit’,…
‘Units’,’normalized’,…
‘Position’,[0.25 0.3 0.2 0.1],…
‘String’,’ ‘,…
‘Callback’,@valueBox_callback);
end
function plusPushButton_callback(hObject,eventdata)
handles=guidata(hObject);
handles.counter = handles.counter + 1;
guidata(hObject,handles);
end
function minusPushButton_callback(hObject,eventdata)
handles=guidata(hObject);
handles.counter = handles.counter – 1;
guidata(hObject,handles);
end
function valueBox_callback(hObject,eventdata)
handles=guidata(hObject);
set(handles.resultBox,’String’,num2str(counter)); %display current value of counter
end Dear All,
I’m trying to create a simple GUI for a counter.
The GUI consists of a plus and a minus pushbutton as well as an edit textbox for displaying the current value of the counter which starts at 1. Unfortunately, the value doesn’t apper in the display textbox. Below is my code, what could be wrong here?
Thanks,
Ben
function counter()
hfig = figure();
guidata(hfig, struct(‘counter’,1)); %set counter default value to 1
plusPushButton = uicontrol(‘Parent’, hfig,’Style’,’pushbutton’,…
‘Units’,’normalized’,…
‘Position’,[0.1 0.5 0.2 0.1],…
‘String’,’+’,…
‘Callback’,@plusPushButton_callback);
minusPushButton = uicontrol(‘Parent’, hfig,’Style’,’pushbutton’,…
‘Units’,’normalized’,…
‘Position’,[0.4 0.5 0.2 0.1],…
‘String’,’-‘,…
‘Callback’,@minusPushButton_callback);
valueBox = uicontrol(‘Parent’, hfig,’Style’,’edit’,…
‘Units’,’normalized’,…
‘Position’,[0.25 0.3 0.2 0.1],…
‘String’,’ ‘,…
‘Callback’,@valueBox_callback);
end
function plusPushButton_callback(hObject,eventdata)
handles=guidata(hObject);
handles.counter = handles.counter + 1;
guidata(hObject,handles);
end
function minusPushButton_callback(hObject,eventdata)
handles=guidata(hObject);
handles.counter = handles.counter – 1;
guidata(hObject,handles);
end
function valueBox_callback(hObject,eventdata)
handles=guidata(hObject);
set(handles.resultBox,’String’,num2str(counter)); %display current value of counter
end gui, counter, guidata, handles MATLAB Answers — New Questions