Open In App

Simple GUI Calculator in MATLAB

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:

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:

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 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:

 

 


Article Tags :