From the course: MATLAB Essential Training
Create a MATLAB script - MATLAB Tutorial
From the course: MATLAB Essential Training
Create a MATLAB script
- [Instructor] You can open MATLAB and perform calculations in the command window, but the program's real power comes out when you record a 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. I'm currently in the command window and one thing that I will do before I switch over to the script editor is to show you where the file is being stored. So I have here the folder MATLAB Drive, which is indicated as the target path or the current path. And I can also see from this icon that it is stored online. And that's because I'm using MATLAB Online to create this course. If I want to change the path, then I can go to the user interface and on the home tab, click set path and use the control to change the search path used by MATLAB to look for files. I don't actually recommend doing that because any solution you create that relies on an external file will look in the current folder. So if you don't copy every file that you need to the new path, which doesn't happen automatically, then you'll get broken references and your scripts will not work. So unless you have a good reason to change from MATLAB Drive, please don't. Okay, with that in mind, let's go ahead to the home tab and click new script to create our script and I'll create a formula to calculate the volume of a sphere. So I'll start by defining the radius, and for this example, we won't worry about any user input and I'll just set it to seven and I'll put a semicolon at the end of the line to suppress output. Now I will assign the volume calculation to a variable called vol. So vol equals four times pi, which is built in, times the radius cubed, so caret three, divided by three, and I will not put a semicolon at the end of the line, so I get the output displayed in the command window. If I want to add comments, then I can do that by typing a percent sign. So there we go. It's displayed in green and anything else on this line will be considered a comment. So I'll say volume of sphere. If you want to add a comment block that spans multiple lines, then you can use the percent and left curly bracket indicator to show that you're creating a comment block. So I'll go down to the next line and say one line of comments, a second line, and another. When you're done creating your comment block, use a percent sign and a right curly bracket to end. The indicators for the start and end of comment block, that is the percent sign, followed by either a left or right curly bracket. Does need to be on its own line. Right, with that in place, I'll press Control + S to save and I will name the file sphvol. The .m extension is added by the program, so I don't need to worry about it. Press enter. And there we go. Sphvol.m appears in my file list. I'll go ahead and close it here in the script editor and in the command window, I can run it by typing sphvol. You see that it appears in the auto complete list. Press enter and I get the sphere volume, 1.4368 times 10 to the third. If I want to edit a file that appears in the files list, I can just double click it and it will appear in the files editor. I'll go ahead and close. If you want to open a recently edited file, you can go to the home tab and then in the file group, click open and you'll see a list of files that were recently created. You can either open them in the script editor or you can open them as text.