How do I update variable values with GUI (continuously with multiple clicks in one session)?

3 views (last 30 days)
Hi, I've posted my code below without the extraneous comments. The code below allows user input of their name, phone number, email address, and numeric input of three questions (as users, hours, intensity, and energypreference). As of now, the GUI is working correctly in storing the input into the workspace, however, I would like the variable values to continuously be updated upon every click of the pushbutton. It seems as though the GUI is just re-setting every time I click the pushbutton.
For example: algorithm calculates g = 6, I would like k = k+g every single time that I click the pushbutton ... meaning k = 0+6 with one click, k = 6+6 with the second click, etc.
function varargout = OneGui(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @OneGui_OpeningFcn, ...
'gui_OutputFcn', @OneGui_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
function OneGui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = OneGui_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
name = get(hObject,'String');
assignin('base', 'name', name);
function edit1_CreateFcn(hObject, eventdata, handles)
function edit2_Callback(hObject, eventdata, handles)
phonenumber = get(hObject,'String');
assignin('base', 'phonenumber', phonenumber);
function edit2_CreateFcn(hObject, eventdata, handles)
function edit3_Callback(hObject, eventdata, handles)
email = get(hObject,'String');
assignin('base', 'email', email);
function edit3_CreateFcn(hObject, eventdata, ~)
function edit4_Callback(hObject, eventdata, handles)
input = get(hObject);
guidata(hObject,handles);
function edit4_CreateFcn(hObject, eventdata, handles)
function edit5_Callback(hObject, eventdata, handles)
input = get(hObject);
guidata(hObject,handles);
function edit5_CreateFcn(hObject, eventdata, handles)
function edit6_Callback(hObject, eventdata, handles)
input = get(hObject);
guidata(hObject,handles);
function edit6_CreateFcn(hObject, eventdata, handles)
function edit7_Callback(hObject, eventdata, handles)
input = get(hObject);
guidata(hObject,handles);
function edit7_CreateFcn(hObject, eventdata, handles)
function pushbutton1_Callback(hObject, eventdata, handles)
users = str2double(get(handles.edit4,'String'));
hours = str2double(get(handles.edit5,'String'));
intensity = str2double(get(handles.edit6,'String'));
energypreference = str2double(get(handles.edit7,'String'));
guidata(hObject, handles);
% ----- Execute calculations of algorithm
clc;
threshold = (users*hours)*1.3^(intensity)/(log(energypreference)+1);
assignin('base', 'a', threshold);
b = evalin('base','a');
if ((users*hours)*1.3^(intensity)/(log(energypreference)+1) < 5)
b = b + threshold;
fprintf('Goodbye. Thank you for using the Automated ShopMate.\n')
else ((users*hours)*1.3^(intensity)/(log(energypreference)+1) >= 5) | ((b + threshold) >= 5)
fprintf('Attention! Please turn on your ShopMate at this time.\n')
end

Answers (1)

Image Analyst
Image Analyst on 27 Nov 2011
You don't do anything with a and b after you change them in your pushbutton callback. Anyway, you shouldn't be storing stuff in the base workspace like you're doing. See the FAQ for better ways. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F

Categories

Find more on Interactive Control and Callbacks 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!