From the course: MATLAB 2018 Essential Training

Create a MATLAB script - MATLAB Tutorial

From the course: MATLAB 2018 Essential Training

Start my 1-month free trial

Create a MATLAB script

- [Instructor] You can jump into MATLAB and perform calculations in the command window, but the program's real power comes out when you record procedure in a script, so you can run it whenever you want. In this movie, I will show you how to create and run a MATLAB script. The first thing you need to do is to make sure that the current folder is what you want it to be. I'm working off the base MATLAB installation, and over on the left side of the program window I see the current folder, and on the application bar here, I see that I'm in my particular account's documents folder and the sub-folder there is MATLAB. Rather than change it, I'll just leave it at that. If you do want to change it though, then you can use the standard windows or your own operating system's method of doing so. So, for example, if you click the down arrow or side arrow here, then you get the list of the other folders that are available under documents. And also if I click curt, I have a number of other places that I could look, including going to the desktop and perhaps going into a separate exercise files folder that I create there. You can see that the documents folder's name is in bold, and that indicates that it is, in fact, part of the current folder's path. So I'll click away from that, and then click escape to get back to the normal display. To create a new script, I will go to the home tab, which is displayed by default, and click the new script button. I could also press control N. When I do so, I get an untitled window. Yours might be titled something different. I've been working in MATLAB for a while now, so my numbers are different from when I just started. And you also see that I have an editor window here up top where I can create my script, and then a command window at the bottom. The command window here works exactly the same way as the command window when you open MATLAB itself. So now I can start creating my script. Let's say that I want to define a formula that calculates the volume of a sphere, and I want to provide all of the values. To do that, I can first start by entering in the radius, which I need for the formula. R-a-d-i-u-s and then an equal sign to assign the value, and then the number seven. Now I don't want to see these value assignments, and that's why you might have noticed this highlight, and the little squiggly red line indicating a potential error around the equal sign. So if I hover the mouse pointer over that, you'll see that the program's telling me to terminate the statement with a semicolon to suppress output. So I'll go ahead and type that semicolon and enter, and you can see that the notification has gone away. Now I will type in the formula itself, and I'll assign it to the variable vol, v-o-l. So I'll type v-o-l equals four times pi, which is a built-in constant, times the radius cubed, so that is to the power of three, and then that's all divided by three. And I don't need to worry about any kind of operations order or operational precedence because it's all multiplication, except for radius raised to the third power, or cubed, so everything should operate exactly as needed. Now I can save my file, and to do that I'll just go up to the editor ribbon tab and click save. Takes me to the documents MATLAB folder, and it's currently untitled2, but I will call it sphvol, and I want to make sure that it is a MATLAB code file, a dot m file. I don't need to add the extension myself, just need to make sure the type is right. It is, so I'll click save, and there is my file. And you also see that it appears within the current folder. If I want to run my script, I can do so from the command window. So I'll go down to the command window and I'll type the name of the file, and I'll leave off the dot m, that's basically treated as an external function. So sphvol and enter, and I get 1.4368 times 10 to the third power. So there is my answer and my script ran correctly. Other things you can do with a script are to add comments. If you want to add just a single line comment, then you can do so by preceding the line with a percent sign. So let's say that I'll go back up to line one, and I will type a percent sign, then a space, and you can see that the text turned green to indicate it's a comment, and we'll just say sphere volume calculation, and enter, and you can also see that the sphere volume calculation comment is in green, and that indicates it's a comment and not active. In other words, it's not a command that will be run. If you want to add multiple lines of comments, you can do that as well. To start you need to type a percent sign, and then a left curly bracket as the next character. So I'll type that, and then enter. The percent sign and left curly bracket needs to be on its own line, and you'll also notice that everything below it is currently considered a comment. That's because we haven't closed the comment block yet. So I'll go to the end of sphere volume calculation and enter, and I'll say Curtis Fry, first script, then enter, now I can close out the comment block, and for that I'll type a percent sign, followed by a right curly bracket, and there you see that I have my comment block and the active code below that in black. Also note that if you want, you can close up your comment block. That's what this hide detail button means here. It's a square with a minus sign in it. If I click that, then the comments are indicated, but they aren't displayed, and if I click the show detail button, then they come back. So if you want to close up your comments, you can do so. However, if you want to expand them so you can read them, that's what you need to do. That's how you do it using the show and hide detail buttons. I've already saved the script, so I will press control S to save it again. That's the keyboard shortcut. You can close your file by going over to the close button at the right side of the editor. So just click the close editor button, it's called, and the file goes away. If you want to open it back up, just go over to your current folder, double click, and it opens back up. So as you can see, working with scripts takes a little bit of getting used to, but it's actually not that difficult.

Contents