![]()
Module 3: User Defined Functions /Connect
C onnecting the MATLAB commands with the ability to isolate the effect of commands into separate files.
We can begin with the information provide by MATLAB's help facilityThe M-file script is a file which name is filename.m which contains any sequence of MATLAB commands. This has been used for your first and second computational experiments.
The MATLAB Search Order is important to keep in mind when you choose names for either your M-file scripts or your M-file functions. When you type any name at the MATLAB prompt (or within a Script or Function), MATLAB tries to find a definition for that name, for this example, supposed the name is cows in the following order:
MATLAB Search Order
The M-file script interacts with the MATLAB workspace. You can always examine the contains of this workspace by type who or whos .
- Variable name
- The current MATLAB workspace is searched. ( whos would list the variable cows)
- Built-in MATLAB function
- If MATLAB has a function with the name cows, then help cows should give the definition, but since MATLAB has no command called cows you'll get nothing.
- A file in the current directory
- If the current directory contains cows.m, then this M-file script will be executed. You can list the files in the current directory by typing what . Optionally, you can type dir or ls to have all the files in the current directory listed.
- A file in the MATLAB path
- You can type path to see the MATLAB path, but be forewarned it is lengthy. You'll notice, though, that the final entry in your path for this course is /home/ma/stewart/cs205/spr98/205stuff . This is due to the way I set up the class accounts before the semester began. You might also have noticed that your account has a directory called matlab which contains the file startup.m . Each time MATLAB starts, this file is executed and you can examine it yourself to see that it resets your MATLAB path to include the pointer to my directory where I place all example referred to in lecture (and in these course notes).
- which
- If you are ever confused by MATLAB's response when you type in a name, you can use the which cows command to find the directory path to cows.m
M-file functions provide greater control over the use of the MATLAB workspace. These functions can be invoked from the MATLAB command line or from M-file scripts or from other M-file functions. They have a specific structure which is defined in the help page entry.The following diagram is trying to highlight the primariy structure which all M-file functions must follow.
cows.m -------------------------------------------------------- | function output_values = cows (input_values) | % comments to describe your function | % this information will be returned anytime you | % type help cows | | MATLAB commands to do whatever cows does | | output_values = whatever | |-------------------------------------------------------
Page author: Dr. Kris Stewart
URL: http://www.stewart.cs.sdsu.edu/cs205/module3/Connect.html
Last updated: Feb. 10, 1998