How can I store value and link three pushbuttons (GUI)

1 view (last 30 days)
Please correct my code. It shows the total number of clicks when I clicked the "click here" button. And then when I click the "add" button, it adds 1. For the "subtract" button as well. My problem is that when I click the "click here" button, it executes properly as well as when I click the add button, it added a value to a current value but after that, when I click the "click here" button again the number of clicks is wrong. Here's my code:
% --- Executes just before VENTURA_MyFirstGUI is made visible.
function VENTURA_MyFirstGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to VENTURA_MyFirstGUI (see VARARGIN)
% Choose default command line output for VENTURA_MyFirstGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes VENTURA_MyFirstGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = VENTURA_MyFirstGUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in clickbutton.
function clickbutton_Callback(hObject, eventdata, handles)
% hObject handle to clickbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
persistent count
if isempty(count)
count=0;
end
count=count+1;
handles.count=count;
set(handles.screen,'String',count);
guidata(hObject, handles);
% --- Executes on button press in add.
function add_Callback(hObject, eventdata, handles)
% hObject handle to add (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA);
persistent add
x = handles.count;
if isempty(add)
add=x;
add=add+1;
else
add=add+1;
end
handles.add=add;
set(handles.screen,'String',add);
guidata(hObject, handles);
% --- Executes on button press in minus.
function minus_Callback(hObject, eventdata, handles)
% hObject handle to minus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
persistent minus
y = handles.count;
if isempty(minus)
minus=y;
minus=minus-1;
else
minus=minus-1;
end
handles.minus=minus;
set(handles.screen,'String',minus);
guidata(hObject, handles);
  1 Comment
Rik
Rik on 21 Jan 2021
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.

Sign in to comment.

Answers (1)

Rik
Rik on 21 Jan 2021
You don't load the click count from the handles struct, so it will overwrite with the persistent variable.
function clickbutton_Callback(hObject, eventdata, handles)
% hObject handle to clickbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
count=handles.count;
if isempty(count)
count=0;
end
count=count+1;
handles.count=count;
set(handles.screen,'String',count);
guidata(hObject, handles);

Categories

Find more on Startup and Shutdown 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!