Table of contents
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.
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
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
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.
We can also check the value each variable has by writing them in the command window and clicking Enter:
But if we try to check a non-existent variable, MATLAB will give us an error:
We can also check the existing variables with who:
who
And more relevant data with whos:
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
If we don't want to print the result or the variables we introduce, we need to put a ; after each command:
x=3;
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;
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;
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:
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
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.
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:
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: