Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions MATLAB’s power comes from functions Functions allow projects to be partitioned into manageable, testable, reusable modules.

Similar presentations


Presentation on theme: "Functions MATLAB’s power comes from functions Functions allow projects to be partitioned into manageable, testable, reusable modules."— Presentation transcript:

1 Functions MATLAB’s power comes from functions Functions allow projects to be partitioned into manageable, testable, reusable modules

2 Function Syntax Function [output arguments] = function_name(input arguments) The word function must be the first word, and must be typed in lower-case letters. A list of output arguments typed inside brackets The name of the function A list of input arguments typed inside parentheses First line of the file must be of the form:

3 Input and Output Values are transferred to and from functions via input and output arguments MATLAB sees only output arguments, not other function variables. Functions see only input variables, not other MATLAB variables.

4 Variables: Local vs Global Usually, once created, variables are available in the workspace until cleared Functions create their own workspace with their own local variables distinct from those in the original workspace –Functions cannot modify variables within the original workspace – except through outputs –Exception – global variables can span both workspaces and be manipulated in both

5 Function files Each function is typically stored in its own m-file. The function file name is function_name.m Each function file is a module that can be tested separately and reused whenever the function is required.

6 Structured Programming Divide and subdivide the project. Write functions for the parts. Manage complexity Debugable Readable Testable Reusable functions Maintainable

7 Example Function: DEG2RAD

8 Another Example Compute the area and perimeter of a right triangle as a function of its base and height: Area = 0.5 * base * height Perimeter = base + height + hypotenuse

9 Example: my personal plotter

10 help and lookfor >> help myplot MYPLOT personal plotting function myplot(x,y,sx,sy,t). Plots y vs x with xlabel = sx, ylabel = sy, and title = t >> lookfor personal MYPLOT personal plotting function myplot(x,y,sx,sy,t). The MATLAB help command prints the comments that occur just after the first line of the function The MATLAB lookfor command prints the comments in the second line of the function, if the line contains the key word.

11 Structured Programming Hierarchical Modules are divided into smaller and smaller submodules (functions work well as modules) functions contain functions Modular Simple independent modules with well defined inputs and outputs Locality Minimize inputs and outputs Generate values inside the module where possible


Download ppt "Functions MATLAB’s power comes from functions Functions allow projects to be partitioned into manageable, testable, reusable modules."

Similar presentations


Ads by Google