How to update value of a variable when a button is pushed? What am I doing wrong in this code?

11 Ansichten (letzte 30 Tage)
I have introduced a variable "value" in the opening function. I want to add a specific number in the same variable when a button is pressed and then want it to update on when next button is pressed. Then I want to display the final value of the variable "value" when pushbutton10 is pressed.
When I run this code, it gives zero as output no matter what button is pressed. Please check this code and tell me what am I doing wrong here? It would be a great help!
function varargout = aaabbb(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @aaabbb_OpeningFcn, ...
'gui_OutputFcn', @aaabbb_OutputFcn, ...
'gui_LayoutFcn', [], ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before aaabbb is made visible.
function aaabbb_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for aaabbb
handles.output = hObject;
handles.value=0;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = aaabbb_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
handles.value = handles.value + 148;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
handles.value = handles.value + 161;
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
handles.value = handles.value + 7;
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
handles.value = handles.value + 0;
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
handles.value = handles.value + 45;
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
handles.value = handles.value + 80;
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
handles.value = handles.value + 82;
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
handles.value = handles.value + 3;
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
handles.value = handles.value + 0;
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
disp(handles.value)

Akzeptierte Antwort

Image Analyst
Image Analyst am 6 Jun. 2020
You need to put a static text label on your GUI with a tag of txtValue (or whatever you want). Then in each callback where you want to change handles.value you need to have this:
handles.value = handles.value + 80; % Or whatever value you want.
handles.txtValue.String = sprintf('The Value now = %.1f', handle.value);
% Update handles structure
guidata(hObject, handles); % Not calling this was your problem.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by