simple calculator using GUI

222 views (last 30 days)
Md. Ashikur Rahman Any
Md. Ashikur Rahman Any on 19 Apr 2021
Commented: Rik on 21 Apr 2021
I tried to make a simple calculator with GUI.
For that purposes i want to take the input as string.then convert this string into number ,and then perfom the operation.
For instance i would like to take input like
("5+6/3")
and then using str2num i want to perform that operation.
In all button i use almost the same code like ( here i show the code for button 1 and divison sign.)
% --- Executes on button press in division.
function division_Callback(hObject, eventdata, handles)
% hObject handle to division (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'/'));
% --- Executes on button press in button1.
function button1_Callback(hObject, eventdata, handles)
% hObject handle to button1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'1'));
In equal button
% --- Executes on button press in equal.
function equal_Callback(hObject, eventdata, handles)
% hObject handle to equal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
p = str2num(s); % it will convert the string in mathmatical expression then perform the operation
set(handles.screen,'string',num2str(p));
It didn't show the expected output.
What to change in equal Button?
Thanks in Advance.

Accepted Answer

Rik
Rik on 19 Apr 2021
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Every callback is a separate function that doesn't know what happened in the other functions. That means the variable s doesn't exist.
function equal_Callback(hObject, eventdata, handles)
s=get(handles.screen,'string');
p = str2num(s); % calls eval implicitly
set(handles.screen,'string',num2str(p));
You should be really careful with str2num. It calls eval, meaning it can execute arbitrary code. You should avoid it when possible. In this case it is safe, as long as the user doesn't type any code in the text box.

More Answers (3)

Ngo Nguyen
Ngo Nguyen on 19 Apr 2021
I think you should use the function: eval
  2 Comments
Ngo Nguyen
Ngo Nguyen on 19 Apr 2021
At first, check your equal_Callback, what is s?
When you start a new function, the parameter "s" is no longer exist?
What kind of the error message did you get?

Sign in to comment.


Md. Ashikur Rahman Any
Md. Ashikur Rahman Any on 19 Apr 2021
function varargout = ExCal(varargin)
% EXCAL MATLAB code for ExCal.fig
% EXCAL, by itself, creates a new EXCAL or raises the existing
% singleton*.
%
% H = EXCAL returns the handle to a new EXCAL or the handle to
% the existing singleton*.
%
% EXCAL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EXCAL.M with the given input arguments.
%
% EXCAL('Property','Value',...) creates a new EXCAL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ExCal_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ExCal_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 ExCal
% Last Modified by GUIDE v2.5 19-Apr-2021 09:57:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ExCal_OpeningFcn, ...
'gui_OutputFcn', @ExCal_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 ExCal is made visible.
function ExCal_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 ExCal (see VARARGIN)
% Choose default command line output for ExCal
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ExCal wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ExCal_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 screen_Callback(hObject, eventdata, handles)
% hObject handle to screen (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 screen as text
% str2double(get(hObject,'String')) returns contents of screen as a double
% --- Executes during object creation, after setting all properties.
function screen_CreateFcn(hObject, eventdata, handles)
% hObject handle to screen (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 button8.
function button8_Callback(hObject, eventdata, handles)
% hObject handle to button8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'8'));
% --- Executes on button press in button7.
function button7_Callback(hObject, eventdata, handles)
% hObject handle to button7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'7'));
% --- Executes on button press in button9.
function button9_Callback(hObject, eventdata, handles)
% hObject handle to button9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'9'));
% --- Executes on button press in button5.
function button5_Callback(hObject, eventdata, handles)
% hObject handle to button5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'5'));
% --- Executes on button press in button4.
function button4_Callback(hObject, eventdata, handles)
% hObject handle to button4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'4'));
% --- Executes on button press in button6.
function button6_Callback(hObject, eventdata, handles)
% hObject handle to button6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'6'));
% --- Executes on button press in button2.
function button2_Callback(hObject, eventdata, handles)
% hObject handle to button2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'2'));
% --- Executes on button press in button1.
function button1_Callback(hObject, eventdata, handles)
% hObject handle to button1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'1'));
% --- Executes on button press in button3.
function button3_Callback(hObject, eventdata, handles)
% hObject handle to button3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'3'));
% --- Executes on button press in zerroo.
function zerroo_Callback(hObject, eventdata, handles)
% hObject handle to zerroo (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'0'));
% --- Executes on button press in dott.
function dott_Callback(hObject, eventdata, handles)
% hObject handle to dott (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'.'));
% --- Executes on button press in pipi.
function pipi_Callback(hObject, eventdata, handles)
% hObject handle to pipi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'pi'));
% --- Executes on button press in sqrtt.
function sqrtt_Callback(hObject, eventdata, handles)
% hObject handle to sqrtt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in square.
function square_Callback(hObject, eventdata, handles)
% hObject handle to square (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in equal.
function equal_Callback(hObject, eventdata, handles)
% hObject handle to equal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
p = eval(s);
set(handles.screen,p);
% --- Executes on button press in resettt.
function resettt_Callback(hObject, eventdata, handles)
% hObject handle to resettt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.screen,'string',' ');
clear all ;
% --- Executes on button press in division.
function division_Callback(hObject, eventdata, handles)
% hObject handle to division (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'/'));
% --- Executes on button press in multiply.
function multiply_Callback(hObject, eventdata, handles)
% hObject handle to multiply (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'*'));
% --- 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)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'-'));
% --- Executes on button press in plus.
function plus_Callback(hObject, eventdata, handles)
% hObject handle to plus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'+'));
% --- Executes on button press in xval.
function xval_Callback(hObject, eventdata, handles)
% hObject handle to xval (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'x'));
% --- Executes on button press in yval.
function yval_Callback(hObject, eventdata, handles)
% hObject handle to yval (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'y'));
% --- Executes on button press in simulate.
function simolte_Callback(hObject, eventdata, handles)
% hObject handle to simolte (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function practice_Callback(hObject, eventdata, handles)
% hObject handle to practice (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 practice as text
% str2double(get(hObject,'String')) returns contents of practice as a double
% --- Executes during object creation, after setting all properties.
function practice_CreateFcn(hObject, eventdata, handles)
% hObject handle to practice (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
  4 Comments
Md. Ashikur Rahman Any
Md. Ashikur Rahman Any on 19 Apr 2021
s is the string what we have give input so far.
like if we press
"5+6"
this "5+6" will be stored in s.
and if i convert this s to number than it should return 5+6 =11
Md. Ashikur Rahman Any
Md. Ashikur Rahman Any on 19 Apr 2021
You can make me correct.What code i should write for equal button?

Sign in to comment.


Md. Ashikur Rahman Any
Md. Ashikur Rahman Any on 21 Apr 2021
I want to add a plot with that calculator like
My plan is to write the expression and converting this string to symbolic variable than plotting the equation.
But it didn't work properly.what to do?
% --- Executes on button press in Plott.
function Plott_Callback(hObject, eventdata, handles)
% hObject handle to Plott (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
grp=get(handles.screen,'string');
global syms x y;
axes(handles.axes1);
x=0:0.1:10;
y=str2sym(grp);
fplot(y,[-10 20]);
guidata(hObject,handles);
  1 Comment
Rik
Rik on 21 Apr 2021
You should not rely on global variables. You have the handles struct right there to share data between callbacks.
You should also consider storing handles.screen.String separately (and restore it every callback that changes it). That way you can avoid malicious input.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices 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!