Trying to make a simple calculator/multiplyer. Only works for the first time.
1 view (last 30 days)
Show older comments
Good day! I'm tryin to make a simple multiplyer that would take 2 input values,sum them up and multiply the sum by 2. The gui has 2 edit windows and 1 push button to request the answer at the static text window. The program works only for the first time,as soon as I try to change the previously input values, it gives me an error in the command window and won't work. I have also changed the string in the edit boxes to 0. Those 0 are string values,not actual values. I tryed the following script to turn them into values,but it just changes them permamently.
% --- Executes just before EditBoxTrial is made visible.
function EditBoxTrial_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 EditBoxTrial (see VARARGIN)
% Choose default command line output for EditBoxTrial
handles.output = hObject;
num = 0;
handles.edit1 = num;
handles.edit2 = num;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes EditBoxTrial wait for user response (see UIRESUME)
% uiwait(handles.figure1);
How do I fix two of those problems? The full program code is
function varargout = EditBoxTrial(varargin)
% EDITBOXTRIAL MATLAB code for EditBoxTrial.fig
% EDITBOXTRIAL, by itself, creates a new EDITBOXTRIAL or raises the existing
% singleton*.
%
% H = EDITBOXTRIAL returns the handle to a new EDITBOXTRIAL or the handle to
% the existing singleton*.
%
% EDITBOXTRIAL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EDITBOXTRIAL.M with the given input arguments.
%
% EDITBOXTRIAL('Property','Value',...) creates a new EDITBOXTRIAL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before EditBoxTrial_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to EditBoxTrial_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help EditBoxTrial
% Last Modified by GUIDE v2.5 10-Jul-2012 09:24:44
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @EditBoxTrial_OpeningFcn, ...
'gui_OutputFcn', @EditBoxTrial_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 EditBoxTrial is made visible.
function EditBoxTrial_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 EditBoxTrial (see VARARGIN)
% Choose default command line output for EditBoxTrial
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes EditBoxTrial wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = EditBoxTrial_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;
function edit_box1_Callback(hObject, eventdata, handles)
num=str2double(get(handles.edit_box1, 'String'));
handles.edit_box1=num;
guidata(hObject,handles);
% hObject handle to edit_box1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_box1 as text
% str2double(get(hObject,'String')) returns contents of edit_box1 as a double
% --- Executes during object creation, after setting all properties.
function edit_box1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_box1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_box2_Callback(hObject, eventdata, handles)
num=str2double(get(handles.edit_box2,'String'));
handles.edit_box2=num;
guidata(hObject,handles);
% hObject handle to edit_box2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_box2 as text
% str2double(get(hObject,'String')) returns contents of edit_box2 as a double
% --- Executes during object creation, after setting all properties.
function edit_box2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_box2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in push_button1.
function push_button1_Callback(hObject, eventdata, handles)
c=(handles.edit_box1+handles.edit_box2)*2;
str=sprintf('Answer is: %d',c);
set (handles.static_text, 'String',str);
% hObject handle to push_button1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
0 Comments
Accepted Answer
John Petersen
on 30 Jul 2012
You're reassigning handles.edit_box1 which loses your handle. Instead, use
x=str2double(get(handles.edit_box1,'String'));
setappdata(handles.figure1,'x',x);
and
y=str2double(get(handles.edit_box2,'String'));
setappdata(handles.figure1,'y',y);
in both the edit boxes, then in the pushbutton function use
x = getappdata(handles.figure1, 'x');
y = getappdata(handles.figure1, 'y');
c = 2*(x + y);
0 Comments
More Answers (0)
See Also
Categories
Find more on Environment and Settings 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!