DEV Community

Cover image for Use MATLAB as a Calculator and Discover how to get Help
Sandra
Sandra

Posted on • Originally published at sandramartin.hashnode.dev

Use MATLAB as a Calculator and Discover how to get Help

After learning about the MATLAB desktop and the use of each window, it is time to begin creating our first programs. In this post, I will talk about how to write simple programs in the command window and how to get help with different functions and commands in MATLAB. Finally, I will challenge you to practice what you've learned. Happy coding!

Using the Command Window

The command window is the window in the middle of the MATLAB desktop.

Command window.png

In this window, we can see the >> symbol. This symbol is called a prompt and means that MATLAB is ready for us to introduce our commands.

As a curiosity, apart from clicking x on the top right of the desktop, we can write quit in the command window, and MATLAB will close.

Stopping infinite programs

Sometimes, we might create a program that runs infinitely. We can stop its execution by pressing CTRL+C. This way, we will stop it and be ready to introduce new commands.

Introducing commands

A significant command to know is clc. It will clear everything we have in our command window.

clc

Enter fullscreen mode Exit fullscreen mode

To create variables, we need to set their name and assign them a value. There is no need to create a variable indicating its type before doing that. For example:

x=2
y=6
z=x+y

Enter fullscreen mode Exit fullscreen mode

In this code, we set x=2 and y=6 and added both variables to find z=8. The computer stores the value of these variables in a portion of its memory.

When we run the code we have written and click Enter, MATLAB will print the value of each variable.

First code.png

We can also check the value each variable has by writing them in the command window and clicking Enter:

Check variables.png

But if we try to check a non-existent variable, MATLAB will give us an error:

Non existant variable.png

We can also check the existing variables with who:

who

Enter fullscreen mode Exit fullscreen mode

who example.png

And more relevant data with whos:

whos example.png

Finally, we can delete the value of one variable or all the variables in the workspace with clear:

clear x % Deletes the x variable
clear % Deletes all variables

Enter fullscreen mode Exit fullscreen mode

If we don't want to print the result or the variables we introduce, we need to put a ; after each command:

x=3;

Enter fullscreen mode Exit fullscreen mode

Same command in different lines and more than one command in one line

Sometimes we may need to write very long commands, and as a consequence, we won't be able to see them in a single row. To solve this, we can use ... and that way, we will be able to continue writing the command in the following line:

x=3 + ...
2;

Enter fullscreen mode Exit fullscreen mode

We might also want to write different commands in the same line. For example, when we assign values to many variables. For that, we use the comma:

x=2, y=5; z=y-x;

Enter fullscreen mode Exit fullscreen mode

If we use a , MATLAB will print the value, but not if we use a ;.

Repeating commands with History Window

After writing some commands, if we click the up-arrow key, we will open the History Window.

In that window, we will see all the commands we have written, and we will be able to select one of those by moving up and down with the arrow keys and pressing Enter:

History Window.png

We can also edit one of those commands by moving up and down with the arrow keys and then just by changing whatever we want from the command.

Getting help from MATLAB

Help function

If we know the command name but need more help with it (know the inputs, outputs, what it does and more), we use the help function. For example:

help clear

Enter fullscreen mode Exit fullscreen mode

help command.png

Search Documentation bar

Sometimes we might know what we want to do but not the command. In this case, we go to the Search Documentation bar at the top-right of the MATLAB desktop.

search documentation bar.png

We can write what we want to do in that bar, for example, plotting, and MATLAB will give us many different options for that, including explanations for each of them:

MATLAB help.png

Challenge

For you to test what you've learned this far, I propose you some little challenges with MATLAB:

Challenge 1:

Can you calculate the following in the command window?

  • 90+6
  • 100-24
  • 3/2

Challenge 2:

We want to buy some products of price 12. For more than three, then they'll give us a 10% discount. What is the price of 2 products? And of 4?

To sum up

In this post, we have learned how to use MATLAB as our calculator. We also learned some interesting details about the desktop, which we didn't cover in the previous post.

Now you can use MATLAB as your own calculator! Happy coding!

Useful links

You can learn more about the MATLAB desktop in this post:

Simple MATLAB Intro. Getting to Know the Interface

Top comments (0)