Today’s lesson will the following topics:
1. Useful Commands
2. Suppressing Outputs using Semicolon
3. Controlling Order of Operations
Useful Commands
(a) “clc” : Clears the Command Window. Check out the 3 images below (read the captions)
(b) “clear all” or “clear” : Clears all variables from the workspace. Check out the captions on the 3 images below.
(c) “ctrl-c” : Aborts a MATLAB computation. See the screenshot below. I have made a script (scripts will be discussed in a future lesson) which is why the Command Window looks to be divided in two. When I run the script, I pressed the ‘Control’ and the ‘C’ keys down together, and it terminated the script before it ran the entire code
(d) “…” : Continues a line. This is so that longer lines of code can be shortened into smaller lines without one long continuous line. See the example below. I have again made a script with an input statement, but the variable “a” had the three dots after it, which meant that the code continued onto the next line. I did not do that for the second variable “b,” and when I run the code, take a look at what happens (read the captions):
(e) “help _____” : The empty space can be any command you need help with. This is an alternative to the Help Browser discussed in a previous lesson, but you need to know the exact function or command you are searching for (eg. If I want to know the command for finding square root of a value, I can’t use this. I would already have to know that it’s the ‘sqrt’ command. If I know this in advance, then I can put it in the help command and get information about it). Check out the captions in the pictures below to understand this example:
Note that it has to be an exact command you are searching for. For example, if I want to search up what the command is for square roots, then I wouldn’t use the help command. Take a look at this example below:
(f) “lookfor _____” : The empty space can be a command (or function) you need help with. This is another way to get help. But with the help command, it searches for the exact name of the command you type in. Here, it will search the summaries of each command to find any matching words, so a series of different commands relating to that command will pop up. Take a look with “sqrt,” the same example we used above. “sqrt” is the command used to find the square root of a number:
(g) “doc ____” : Once again, the empty space is a command you enter that you need help with. This will pull up the online documentation for that command and can be useful with more complex commands. Here, you don’t need the exact name of the command or function. If I want to know what command to use for the square root of a number, I just type in “doc square root,” like I do below:
(h) “diary ____” : “diary _____” starts saving everything from that point forward, and “diary off” stops the recording. All code in between these two commands will be saved to a text file, and in the blank area you will add in the name of what you want the file to be saved as. This won’t save as a MATLAB file because we have not made scripts or functions (will talk about these in a separate lesson) but if you just want to save what was done in the Command Window, you can use this command.
It will be .txt and will automatically save to the MATLAB folder (the location of the folder is shown at the top of the MATLAB window, I have highlighted it in the screenshot below):
Entering Multiple Statements Per Line can be done with commas in between the statements.
Suppressing Outputs using Semicolon
Suppressing outputs can be done by using semicolons after each command. This means that the output is not shown when you hit ‘enter’ after entering the command. As you can see in the example below, nothing appears in the command window after I create the variables a=5 and b=7.
However, the picture right below here shows that if I don’t add a semicolon, the outputs are shown:
Controlling Order of Operations
In doing calculations, MATLAB works in the order of BEDMAS – Brackets, Exponents, Division/Multiplication and then Addition/Subtraction. This means that from left to right in the equation, those operations are executed in order. If you want to change the order they are executed in, you will need brackets because they are first in BEDMAS. For example, take a look at the screenshot below: