Video: Script m-files

 

 

Transcript

Today I will talk about script m-files.

A script m-file is simply a text file which contains a sequence of commands that you can execute all at once, just using a single click or a single command. You can save and modify a script m-file as needed. Before I show how to create a script m-file, let's do a simple exercise in the command window.

 

Suppose we need to calculate the volume of a cyclinder with given radius and given height. Say radius equal to 2 and height equal to 3 units. Then in command window I will type r equal to 2, and h to equal 3. r and h are variable names for radius and height. And then volume is pi times r squared times h. I can give this a name as well. Say V. So volume is calculated. Now suppose I want to know volume

for a different radius and a different height. Say r equal to 5 and h equal to 3. Then again find volume using the same formula again. Well if you need to know volume with several differnet radii and different heights, then it is going to be tedious. So this is where a script m-file will be useful.

 

There are 2 ways to create a script m-file. Usual way is to click on the top left corner, New Script. If you

click over there, a new window pops up, which is actually a blank new script m-file. You can type in the ommands and save it and run it.

 

Okay. let me tell you another way as well. The other way is useful when you have already typed commands in the command window. What you do - you can go to Command History and just select those commands that you want in your script m-file so I want this, and this and this, these three.

Right click, create a script. You can see a new window popped up which already has those 3 commands in there. You can further edit it. So it is always good to use clear all command in the beggining. And clc as well, so you don't have any previous output or previous command in the command window, you just get fresh output from you script m-file when you run it. Also it is a good idea to have a comment in the beginning why this file is for. So it is to calculate volume of a cylinder.

 

Now I'll show you how to save it. So you can click on save as... and you can browse to where you want to save it so, if I want to save in my flash drive, I will click over there. I need a new folder, click over there. You can name your folder - My m-files or something. And then I can name this file as well. Script m-file names has certain rules which are the same as variable naming. So file name should start with a letter, followed by numbers and letters. You can use under scores but no spaces. All those rules. So I can name it something ... suppose I want to name it Myfile1 dot m is always the extension. And save. So it is saved. You can see where it is saved. It is the path here. It is Mymfiles and myfile1.m.

 

Ok. now how to run this program or script m-file? It can be done in 2 different ways. One way is to click on the screeen button run. Or, type the file name into the Command window without any extension dot m. So whichever way you choose to execute this script m-file, the file should be in current folder. At the moment it is not. There are 2 ways to do it. The simplist way is to just click on the screen button run

and if the file wasn't already in the current folder, it will prompt you to change folder. Click over there.

The file is now in the current folder, as well as it is executed for you. Let's see. The previous things are all cleared and you have just output from this program.

 

Now as I said, another way to run the script m-file, is just type in the Command window the file name without any extension and press enter. So the file name was myfile1 dot m, so no need to put dot m

there. Just press enter. So it is executed. And you don't see any difference from previous output, obviously because it is the same program. It is important to note that typing the file in the Command window will not work if the file is not in the current folder. So you should know how to manually change the current folder. Here is a small hint. On the top left corner there is an icon, browse for folder. Click over

there and then you can browse to where ever your folder is. Just select that folder and the current folder is changed to that folder.

 

 

Last modified: Monday, 10 August 2015, 10:20 AM