How to store values from push button and then add all values and display in a static text box
9 views (last 30 days)
Show older comments
I want to make a calculator in MATLAB GUIDE which stores the values of buttons when pressed in a variable and then add the values after each press and display the final value of variable in static text box after pressing the 'CALCULATE' push button. my gui looks like this
This is the code I have made so far. but it displays everything like a calculator. I only want a final answer to be displayed after addition without having to use the 'plus' button.
TIA
function varargout = untitled(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton3_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'200']);
function pushbutton4_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'200']);
function pushbutton5_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'200']);
function pushbutton6_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'200']);
function pushbutton7_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'200']);
function pushbutton8_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'200']);
function pushbutton9_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'8']);
function pushbutton10_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',num2str(eval(get(handles.text2, 'string'))));
function pushbutton11_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'+']);
function pushbutton2_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'21']);
function pushbutton1_Callback(hObject, eventdata, handles)
set(handles.text2, 'string',[get(handles.text2, 'string'),'12']);
2 Comments
Rik
on 4 Jun 2020
I edited your code to take out the bloat comments that GUIDE adds to improve the readability.
You should probably consider moving away from GUIDE. For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
If you want something to happen when you click the calculate button, what is stopping you from putting that in the callback of that button?
You should probably consider making one callback for each category. Then you can more easily replace entries if the user wants to change a selection. Storing everything in a String property is fragile to say the least. You should consider using the guidata struct to store the selection.
Accepted Answer
Rik
on 4 Jun 2020
If you're using GUIDE for the first time: don't. Do not teach yourself a tool that will be removed from Matlab in one of the next releases. For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
To answer the question in your comment:
%in your startup function initialize handles.ValueInSlots
function callback_machine_size(hObject,~)
%this is the callback for the machine size category
str=get(hObject,'String');
handles=guidata(hObject);
handles.ValueInSlots(1)=str2double(str);
guidata(hObject,handles)%store back to figure
end
function callback_knotting_type(hObject,~)
%this is the callback for the knotting type category
str=get(hObject,'String');
handles=guidata(hObject);
handles.ValueInSlots(2)=str2double(str);
guidata(hObject,handles)%store back to figure
end
function callback_calculate(hObject,~)
handles=guidata(hObject);
val=sum(handles.ValueInSlots);
str=sprintf('%d',val);
set(handles.text_field,'String',str)
end
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!