- •Ministry of education and science, youth and sport of Ukraine
- •Theory MatLab programming Kinds of m-files
- •M-file structure
- •Providing help for programs
- •Creating m-Files: Accessing Text Editors
- •Scripts:
- •How functions work
- •Checking the number of function arguments
- •Passing variable numbers of arguments
- •Local and Global variables
- •Persistent Variables
- •Special Values
- •Figure 4 Flow control ‘switch’
- •Conclusion
- •References
Creating m-Files: Accessing Text Editors
M-files are ordinary text files that you create using a text editor. MATLAB provides a built-in editor, although you can use any text editor you like.
Another way to edit an M-file is from the MATLAB command line using the edit function. For example, edit foo opens the editor on the file foo.m. Omitting a filename opens the editor on an untitled file.
You can create the fact function shown on the previous page by opening your text editor, entering the lines shown, and saving the text in a file called fact.m in your current directory.
Once you’ve created this file, here are some things you can do:
• List the names of the files in your current directory
what
• List the contents of M-file fact.m
type fact
• Call the fact function
fact(5)
ans = 120;
Scripts:
If we wish to execute repeatedly some set of commands and possibly change input parameters as well, then one should create a script M-file. Such a file always has a “.m” extension, and consists of the same commands one would use as input to a terminal.
Simple Script Example
Here is the example of simple script with the name “my_plot” (fig. 1)

Figure 1 Script “my_plot”
This script is representing the graph of the equation y=x.*sin(3*x.^2).*exp(-x.^2/4). This script can be called from the command window with writing command “my_plot”.
Functions
Most of the M-files that one ultimately uses will be function M-files. These files again have the “.m” extension, but they are used in a different way than scripts. Functions files have input and output arguments, and behave like FORTRAN subroutines or C-functions.
Simple functions example
Here an example of simple function “my_fun” (fig. 2a) which has 1 input argument “x” and represents the graph with the help of script “my_plot” (fig. 2b):
|
Figure 2a Function “my_fun” |
Figure 2b Script “my_plot” |
Basic parts of a function M-file
A function M-file consists of (fig. 2a):
The Function Definition Line
The H1 Line
Help Text
The Function Body
Comments
Function names
MATLAB function names have the same constraints as variable names. MATLAB uses the first 31 characters of names. Function names must begin with a letter; the remaining characters can be any combination of letters, numbers, and underscores. Some operating systems may restrict function names to shorter lengths.
The name of the text file that contains a MATLAB function consists of the function name with the extension .m appended. For example, average.m
If the filename and the function definition line name are different, the internal name is ignored.
Thus, while the function name specified on the function definition line does not have to be the same as the filename, we strongly recommend that you use the same name for both.
How functions work
If no output argument is supplied, the result is stored in ans. If the second input argument is not supplied, the function computes a default value. Within the body of the function, two quantities named nargin and nargout are available that tell you the number of input and output arguments involved in each particular use of the function. The rank function uses nargin, but does not need to use nargout.


