Announcements | Syllabus | Schedule | Weekly lecture notes | Assignments | Links |

The following lecture and demonstration of scripts and functions illustrate the real power of Matlab and the opportunity to get it to do some work for you. You can create these files in the text editor. They should be saved with a .m extension, and then run by typing their name (and adding arguements in the case of functions), but without the .m at the end.

Here is a simple script that will say Hello:

%simple script to say hello world hello.m fprintf('HELLO WORLD!\n');and how I ran it and the outcome:

>> hello HELLO WORLD!The function

>> fprintf('here is a number = %.3f (that was the formatting)\n',2.5) here is a number = 2.500 (that was the formatting)

The main points are that you put a formatted placeholder in the line (%.3f) which in this case says to format the number as a 3 decimal place floating point. The \n tells Matlab to start a new line. Note the newline and the formatting placeholder along with the text are in the quotes. Then the number follows and is placed where it should be. If you have more than one placeholder, Matlab puts the numbers in sequentially and you can actually evaluate an expression there:

>> fprintf('here is a number = %.3f (that was the formatting), and here is its half = %.\n',2.5,2.5/2) here is a number = 2.500 (that was the formatting), and here is its half = 1.250Video explanation of simple script and fprintf

"When you use Matlab functions such as inv, abs, angle, and sqrt, Matlab takes the variables that you pass it, computes the required results using your input, and then passes those results back to you. The commands evaluated by the function as well as any intermediate variables created by those commands are hidden. All you see is what goes in and what comes out (i.e., a function is a black box)."--p. 143 from Mastering Matlab 5.

functions | scripts |
---|---|

can take input arguments | no input |

ONLY work with variables created WITHIN the function or PASSED to the function as input arguments (functions have their OWN workspace) |
operate on any variables within the Matlab workspace |

all variables EXCEPT output arguments defined by the function are NOT retained in the Matlab workspace after completion |
any variables created throughout the script stay in the Matlab workspace until cleared by the user |

function r = DegreesToRadians(degrees) %this function takes an input angle in degrees and converts it to %radians %JRA, 11.22.99 r = degrees * (pi/180);Here is some Matlab action associated with it:

>> ls ans = DegreesToRadians.m >> help DegreesToRadians this function takes an input angle in degrees and converts it to radians >> DegreesToRadians(180) ans = 3.1416Video explanation of simple function

Video explanation of function help and function vs. script

Let's make a function and plot it.:

- First line:
**function declaration line** *Help*information: the very next part needs to be a comment section. It will be displayed when you type:- Name of the function
- Description of what the function does
- Define the input arguments
- Define the output arguments
- An example of how it is used (optional)
- Author's Name
- Commands to run within the function
- You MUST define the output argument as a variable in the function

function output = FunctionName(input);

help FunctionNameRemember to include:

NOTE: you have to be in the directory in which the function is or type its complete path or have the path to that directory added to your Matlab search path

Apply what you have learned in this lecture to very basic programming and river discharge: River discharge function

Last modified: October 13, 2015