Simple GUI Calculator in MATLAB
Last Updated :
08 Mar, 2022
MATLAB is a powerful programming language that makes working with different mathematical equations easier. It is widely preferred by engineers and scientists. In this article, we will see, how to build a GUI-based simple calculator in MATLAB, which will take input and will return a value. So let's get started.
Step 1: To start working with the app, Open the editor, click on the Apps menu, and then on the Design App option.

The app designer, the workspace will be opened. Here you can choose templates for your app. We are going to implement it from scratch, so go Blank App option. Now, our working space will be opened. Let's understand our workspace first before moving forward with our app.
Step 2: In the leftmost part of the workspace, you can find the Component Library, you can select any of the components you want to insert in your app. You just need to drag the component and drop it in the workspace.

Step 3: Workspace IN the center you can find the workspace. It reflects the app you are working on. It consists of two parts:
- Design View: You can design your app from here, like the positioning of different components, etc.
- Code View: You can make your app functional by adding a few lines of code to it. All the code that is added, is done from here.

Step 4: Component Browser When you insert any component to your app and want to study or change its property, then you can do it from here. It controls naming to the description of the component in it.

Step 5: Now to make the interface of your app follow these:
- Drag and drop, 3 Edit Fields(Numeric), for storing two values and one for the answer.
- Now rename and arrange them using drag and drop.
- Now insert 7 buttons, namely for addition, subtraction, multiplication, division, square, square, and a clear button. We are going to work on these all.
- Add a label to the app for a better look.
- Also in the component Browser, search Interactivity for Answer_field and turn off editable.

- Arrange them with your preferred design, here we have designed it in this way,

Step 6: Now the interface of our app is ready. For adding code for any of the buttons, right-click on the button, select callback, add a new pushed_back_func.

Let's add the code now, to handle our buttons.
Example:
Matlab
% MATLAB code for Callbacks that handle component events
methods (Access = private)
% Button pushed function: AddButton
function AddButtonPushed(app, event)
% Getting value from field 1
a = app.Value1EditField.Value;
% Getting value from field 2
b = app.Value2EditField.Value;
c = a + b;
% Displaying answer
app.AnswerEditField.Value = c;
end
% Button pushed function: SubtractButton
function SubtractButtonPushed(app, event)
a = app.Value1EditField.Value;
b = app.Value2EditField.Value;
c = a - b;
app.AnswerEditField.Value = c;
end
% Button pushed function: MultiplyButton
function MultiplyButtonPushed(app, event)
a = app.Value1EditField.Value;
b = app.Value2EditField.Value;
c = a * b;
app.AnswerEditField.Value = c;
end
% Button pushed function: DivideButton
function DivideButtonPushed(app, event)
a = app.Value1EditField.Value;
b = app.Value2EditField.Value;
c = a / b;
app.AnswerEditField.Value = c;
end
% Button pushed function: ClearButton
function ClearButtonPushed(app, event)
% Clearing the previous value to 0
app.Value1EditField.Value = 0;
% Clearing the previous value to 0
app.Value2EditField.Value = 0;
% Clearing the previous value to 0
app.AnswerEditField.Value = 0;
end
% Button pushed function: SquareButton
function SquareButtonPushed(app, event)
a = app.Value1EditField.Value;
app.Value2EditField.Value = 0;
c = a.^2;
app.AnswerEditField.Value = c;
end
% Button pushed function: SquareRootButton
function SquareRootButtonPushed(app, event)
a = app.Value1EditField.Value;
app.Value2EditField.Value = 0;
c = sqrt(a);
app.AnswerEditField.Value = c;
end
end
Output:
Similar Reads
Create a Simple App Using GUIDE in MATLAB
MATLAB is a (Matrix-Laboratory), matrix-based programming language platform that is majorly used to solve math work and real-time problems. it's specifically designed for engineers and scientists to analyze and design systems. And also capable to solve real-time problems with some histogram equaliza
3 min read
Calculus in MATLAB
Calculus is important in different fields like engineering, physics, and other sciences, but the calculations are done by machines or computers, whether they are into Physics or engineering. The tool used here is MATLAB, it is a programming language used by engineers and scientists for data analysis
9 min read
How to Calculate Variance in MATLAB?
Pre-requisites: Calculate Variance In statistical mathematics, Variance is the measure of dispersion of a given data from its average value. This is a very important quantity in statistics and many other fields like Machine Learning and AI. Variance in MATLAB:MATLAB provides a simple function to ca
2 min read
Factorial in MATLAB
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. In this article, we'll be calculating the factorial of a number n using MATLAB's built-in function 'factorial(number)'. Factorial:The
1 min read
How to Calculate the Impulse Response in MATLAB?
Impulse response δ(t) is defined as the output signal that is the result of applying an impulse to a system. The system could be signal filter as well. Physical understanding of the impulse response of a system is highly useful for understanding a dynamic system. In this article, we shall see how to
2 min read
Dates and Time in MATLAB
MATLAB provides many ways to deal with date and time in form of DateTime, calendar duration, and duration data types. These data types do not only support storing and representing date-times but, also allow operations on date time. We shall look at these three data types separately. DateTimeThe date
2 min read
How to Use GNU bc (Basic Calculator) in Linux
The Basic calculator (bc) is an arbitrary-precision calculator that you can use as a simple scientific or financial calculator on the command-line interface of your Linux system. The syntax is similar to the C programming language. You just have to type the following command to see whether bc is alr
3 min read
User-Defined Classes in MATLAB
In this article, we will understand User-defined Classes in MATLAB. Object-Oriented Programming:The object-oriented program design involves: Identify the components of the system or application that you want to build.Analyzing and identifying patterns to determine what components are used repeatedly
3 min read
Scripts and Functions in MATLAB
In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
2 min read
R Program to Calculate Simple Interest
Basically, Simple Interest is a quick method to calculate the interest charge of a loan. It is determined by multiplying the principal amount by the daily interest rate by the number of days which elapse between the payments. It gets estimated day to day-with the help of mathematical terms.we will c
2 min read