Lecture 2: The Command Prompt

Flash and JavaScript are required for this feature.

Download the video from iTunes U or the Internet Archive.

Description: This video lecture continues to explore MATLAB's command prompt and function library, as well as provides an introduction to plotting and creating for and while loops.

Instructor: Yossi Farjoun

YOSSI FARJOUN: This is Dr. MATLAB, lecture two, The Command Prompt. If you're like me, you'll find yourself on the command line a lot when you're working MATLAB, so it's important to know how to get around on the command prompt. The first thing I want to show you is how to use the arrows. Say I want to plot something.

So I want to plot sin of 2x between 0 and pi on 200 points. This is what I type. Of course, I typed it with a typo. I forgot to put the multiplication sign between the 2 and the x up here. So we've already seen in the previous lecture that instead of typing the whole thing, I press the up command, the up arrow, which brings up the previous command. And then with the left arrow, I can place the cursor where I need it to be and insert the asterisk.

I forgot to put the semi-colon, so again, I press up at the semi-colon and now I have what I wanted. Now I plot x and y and I get my plot. I'll put it into the window. Here it is. Great. Now unlike this plot, I want to see the points, so I'm going to press up and modify the plot command. And now I see the point. And I still don't see the point because I plotted to many. So I, again, pressed the up command. I know I can press the up command twice to reach this line, but what I want to show you is what happens when you press the up command after I started typing x equals. See this x equals is the same as this x equals here. When I press up, the first line that starts with x equals will show up. Here it is, if I press up again, it will be the one without the semicolon. If I press up again, it will be the one without the asterisk. Let me go back down to the one I want. This is the one I want.

Now I want to change this 200 to something smaller, say five. So I could press the left area until I reached there, and put 50 points. And now plot again p, up, plot. Now 50 points. But here is a faster way to get to that 50. Again, x, up-- now I want to get to the 50, so instead of moving with the left, I will move with control left, and that moves one word at a time. In this case, it's not a big difference, but sometimes it can be. See, it skips the whole word. It's skipping pi, it's skipping linspace, it's skipping the whole 50. Let me put a 20 here. And now I want to plot red points. And I have that.

Another thing you can do on the command line is to use Control-A. Control-A for the beginning of the line, to move the cursor to the beginning of the line, and Control-E to move the cursor to the end of the line. Control-A for the beginning of the line, Control-E for the end of the line. You can also cut and paste. With a shift, I select Command-C to copy. Let's start a new line, Command-V to paste. Depending on the operating system, those commands might be a bit different.

Another thing you might want to look at is the tab completion property. So if I start typing linspace, I type lin s and now I hit Tab. MATLAB will show all the commands it knows that start with Lin s, in this case, linsolve and linspace. Of course the one I want is linspace. If I want to find help on this, I can press Control-A, move to the beginning of the line, add help, add help, and then hit Enter. Of course I can't see anything, so I'll close this window. And here is the help on linspace. And these are the two ways you can use linspace. You give it the lower and the upper limit, that gives you 100 points, equally spaced between those two. Or you give the lower and upper limit and how many points you want, and it get you that many points. For example, linspace, 0 to 10, five points. Here are five points equally space between 0 to 10.

Another thing that the help file shows you are related commands that you might want to read up on. So I encourage you to read up on logspace and the colon function-- it's actually the colon operator, which is a very complicated help file-- and see why linspace is related to colon and logspace.

Another important feature of the command line is the use of Control-C. So it has two main things. For example, if you have typed something, let's say I've typed this, and I've decided I don't want any of it, I actually went to do something else. So what I should do is clear the whole line. I don't even want to execute this. So to clear the whole line without executing it, I'm going to press Control-C. That clears that line. Control-C can also be used to abort certain operations. For example, if I say x equals 1 while 1x equals x plus 1.

So this is going to add x all the time. As you see, x is going to be growing and growing and growing and growing forever. Now I want to stop this at some point. So to stop it, really the only thing I can do is to press Control-C. You see while it's running, MATLAB says that it's busy. Sometimes it will be busy and you won't even see it. Let's try that again. X equals 1 while 1-- now I'll do it with a semicolon, so I'll suppress output, but it will still be doing it. End. So now MATLAB is busy. It's not typing anything on the screen, but it's doing the same calculation that you saw before, except it's not printing it out. Again, to abort this, we press Control-C. You find out what x we got to. We counted quite high. We counted much higher than here because we didn't have to display.

If you're typing some other command with a multi-line, i equals 1 to 100, j equals i plus 2, for example. And midway through that statement, you've decided that you don't want to do it, again just as before, pressing Enter is not going to help you. You're not going to get a command prompt in this case. You can end it with an end because you started with a for, or you could just press Control-C.