### How to save and execute a file.

First, if you will be using matlab on the same server you want to log in to that machine and create two directories in your home directory: one called "matlab" and the other called "Mat1062". If you're in the math department, you'll be working on the machine called "sphere". If you'll be using matlab on random machines in a computer lab then you want to have a flash stick or some other memory medium. On that stick you'll make two directories: "matlab" and "Mat1062". First save the files junk.m, counter.m, and magic.m in the Mat1062 directory.

Open matlab and type the command "magic(3)". This will produce a 3x3 magic square.

The file "junk.m" is a script file that solves a linear algebra problem, Ax=b, in two different ways. Execute this file by typing "junk" at the matlab prompt.

When you start matlab, it will be in the wrong directory for your purposes. You saw the error message "Undefined function or variable `junk'." when you typed "junk". If this happens, type "pwd". This will show you what directory you're presently in. Move to the correct directory with the command "cd d:" (to get to the d drive, if that's the name of the flash stick drive) followed by "cd Mat1062". To see what's around, type "ls" (or "dir" if on a PC). If you see junk.m, counter.m, and magic.m then you've arrived.

Now try executing "junk". It should work. Now type the command "magic(3)". Note that rather than getting a 3x3 magic square you just got the number 12.

The file "counter.m" is a file that creates a vector x, which is those integers from a to b. Open matlab and execute this file by typing "[x] = counter(3,50)" at the matlab prompt. (Or whatever numbers you choose for a and b.)

Matlab looks for files that end in ".m". When you create a file for matlab, it must end with .m To execute the file, you type the part of the file name that comes before the .m Your file names can have pretty much anything in them, but only one period. (You can't have a file called "test.this.m".)

You have to be careful in the naming of .m files since there are some names already used by matlab. For example, "magic.m" is a built-in file that creates magic squares. If you create your own file called magic.m then what happens when you type "magic(3)"? When you type a command, the first thing matlab does is looks in the current directory. So if there's a file "magic.m" in the directory Mat1062 and if you're in that directory (as shown by "pwd") then it will execute that file. If there is no such file then the second place it will look is in the matlab directory in your home directory. If there is no such file then the third place it will look is in its reserves of files. And so when you started matlab in the first place and typed "magic(3)" it got to the third step and executed its own version of magic.m . Whereas once you were in the Mat1062 directory it executed your own version of magic.m

Why would you ever want different *.m files that all have the same name? In practice if you're doing experiments and you have a lot of data, you'll have directories and subdirectories and subsubdirectories with different experiments in each subsubdirectory. And in each of those subdirectories you might have a file called "make_plot.m" which will load data and make a plot. (This is an excellent habit to get into, btw, because it's not uncommon for referees to deliver reports that ask for minor modifications of plots. If you made that plot by hand six months ago, you may have no idea how you did it. But if you saved all the commands in "make_plot.m" then you're happy as a clam.)

Note: if you're on a PC or a mac then you may have to inform matlab about the matlab directory in your home directory. This means that you have to add that directory to matlab's "path". (The path is the list of places that matlab looks.) To do this, on a mac, in matlab go to "file" and then choose "set path..." and then add the folder matlab in your home directory and click "move up" so that it's the first place it looks after failing to find something in the current directory.

In general, if you're thinking of creating a file with a particular name and you want to test whether or not matlab already has a command with that name, to test if your filename is a good one, say "my_dog_fluffy.m", type "help my_dog_fluffy" at the matlab prompt. If matlab has such a file built in, it will tell you about it. (If you type "help magic" it would tell you all about magic and how to use it. It will give you the names of other commands you might be interested in.) Note: matlab has the annoying habit of doing things like producing the following when you use the help command:
>> help magic
MAGIC Magic square.
MAGIC(N) is an N-by-N matrix constructed from the integers
Reading this, you'd think that to execute the command you should type "MAGIC(3)", wouldn't you? Noooooooo... it's all in lowercase. For some reason, matlab's built-in functions are all lowercase but then they're referred to in uppercase when you ask after them.

The files "junk.m" and "counter.m" are of different types:

junk.m is a script file, which is a file that contains a sequence of matlab commands. If you're going to have to type the same 10 commands in a row, but with minor modifications each time, then it's fastest to write a script file, like junk.m, and then use an editor to type those minor modifications in and then rerun junk.m, rather than typing the 10 commands over and over again.

counter.m is a function file. It takes two inputs, the integers a and b, and gives you back a vector, x. Now that you have x, you can do all sorts of things with it.