Open In App

Simple GUI Calculator in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Component Library

 

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.

Component Browser

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:

 

 



Last Updated : 08 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads