Useful Commands, Controlling Order of Operations, Suppressing Outputs

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)

I have created two variables here
I now typed “clc” as the last prompt, but I haven’t hit enter yet
When I hit enter, the command “clc” is run, and clears the Command Window. Now that area is empty, but the workspace will still have the same variables.

(b) “clear all” or “clear” : Clears all variables from the workspace. Check out the captions on the 3 images below.

Here I have created two variables and they appear in the workspace
Now I typed in “clear all” but haven’t hit enter yet. This can also be “clear”
When I hit enter, the “clear all” command is run, and it clears all variables from the workspace. It does not clear out the Command Window though, so you can still see the previous commands

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

Running the code in the script asks me for a number in the Command Window, which is what the first line of my code does
When I enter a number in the first block, it then should go to the next section of the code, where it asks for a second number. This statement does not work (you can see the error appears in red), even though it’s the same statement as before. This is because I did not use the triple dots to indicate that the code continued onto the next line. So it did not read both lines together as it did for variable ‘a’!

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

I typed in “help sqrt” but have not yet hit enter. This should give me help on the “sqrt” command
When I hit enter, it brings up information about the “sqrt” command

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:

Say I wanted to figure out what command I would use to find the square root of a number, so I type in “help squareroot.” I have not yet hit enter to run the command.
When I hit enter, the help command runs and it gives this output. Because ‘squareroot’ is not the exact name of a command or function, it won’t find anything. You have to already know the exact name of the function you’re looking for before you put it in the help command.

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

I have not yet hit enter, but I have entered “square root” as the phrase I want to get documentation on
When I hit enter, the doc command runs and this second window pops up with documentation relating to “square root”

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

Here I have the first line of code as “diary mynewfile,” so everything I type after this will be saved in a text file called “mynewfile.” Then a little later down I typed “diary off,” so anything after that is not saved in the file
Opening the MATLAB folder shows this file saved there as “mynewfile”
Double clicking it and opening the text file shows everything I have coded in between the “diary mynewfile” and “diary off” lines in my code

Entering Multiple Statements Per Line can be done with commas in between the statements.

Entering commas in between a=5 and b=7 outputs them both at the same time

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.

If you enter semicolons after the commands a=5 and b=7, it suppresses the output so even when I hit enter, nothing was shown in the Command Window

However, the picture right below here shows that if I don’t add a semicolon, the outputs are shown:

Now adding a semicolon right after b=7 but not a=5 will suppress b=7, so only a=5 is shown when you hit enter

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:

MATLAB solves this by going left to right in the order of BEDMAS. This means it does 5/6 first, then adds 4.