Matlab allows you to write your own functions. You can execute these just like any other function in matlab. These functions exist in files whose name ends in a ".m", and are therefore called m-files.
Most of the functions that are used in matlab are actually just m-files. Therefore, you can see how they work and modify them to suit your needs. To list a m-file, just
where fname is the function you want to look at. As an example look at,
Note: You can also see this by typing at the athena% prompt:
Let us examine the structure of this file:
- The first line begins with the word "function" This tells matlab that this is function. If you leave this out, matlab will treat the file as a "script" (see below).
- Following the function declaration there is a list of output arguments (in this case 1), a statement of the function name, and a list of input arguments. Output arguments are listed in the format, [ist:V1,V2,V3... etc] (if you have only 1 you don't need the brackets). The name you use must be the same as the filename (minus the .m). The input arguments are listed after the function name in the format (x1,x2,x3,...etc) These variables are assigned values when the function is called. By default all variables in m-files are local, so you may call them whatever you want.
- After the function declaration, there are a series of lines that start with "%" (these are comments). The first contiguous group of lines that begin with "%" are treated as the help blurb for the function. You can see this by,
When you write your own files, it is useful to use this "help" facility to remind you of its syntax and purpose.
Here is an example of a simple m-file that will scramble the elements in a vector, and output both the scrambled vector and a vector containing indices used to descramble the vector.
If you leave out the "function" line, then a mfile is considered to be a "script". Calling a script is equivalent to entering each of the lines in the script at the ">>" prompt. All variables defined in the parent workspace are accessible to the script and can be modified by the script.
In this sense, all variables are considered global in a script. This feature is both handy and dangerous. You don't have to explicitly pass arguments to a script, but if you assign a value to a variable in a script that exists in the parent workspace, its value is overwritten.
- Vectorize wherever possible to save immense amounts of computation time.
Wherever possible, use matrix operations rather than loops.
- Functions can take variable numbers of arguments. "nargin" and "nargout" are permanent variables that contain the number of input and output arguments.
- You can do almost anything in a mfile that you can do from the matlab prompt including; Loading and saving, issuing system commands ("!command", see ">> type print"), writing to files (fprintf), call other mfiles (including itself), plot, etc..
- An easy way to call programs written in other languages is:
- Save variables in a file
- Run external program which reads the file and writes output to another file.
- Load the data back in.
- Editing a mfile causes matlab to recompile it the next time it is called in matlab. You can, therefore, run a function, modify the mfile, and then run the new version.
Note: Modifying a mfile during the execution of the function does not affect the current process.
- Use the Matlab debugging commands to help track down programming errors. For more information, see How can I debug a .m file?