Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- User Defined Functions in MATLAB pa... by Shameer Ahmed Koya 883 views
- MATLAB Programming - Loop Control P... by Shameer Ahmed Koya 907 views
- User Defined Functions in MATLAB Pa... by Shameer Ahmed Koya 786 views
- Introduction to Matlab Scripts by Shameer Ahmed Koya 1282 views
- Anonymous and Inline Functions in M... by Shameer Ahmed Koya 1249 views
- Conditional Control in MATLAB Scripts by Shameer Ahmed Koya 852 views

1,136 views

Published on

Code for a function is done in an Editor window or any text editor same way as script and saved as m-file. The m-file must have the same name as the function.

Published in:
Education

No Downloads

Total views

1,136

On SlideShare

0

From Embeds

0

Number of Embeds

539

Shares

0

Downloads

27

Comments

0

Likes

1

No embeds

No notes for slide

- 1. User Defined Functions in MATLAB Part 1 Shameer A Koya 1
- 2. Introduction O User-defined functions are similar to the MATLAB pre-defined functions O A function is a MATLAB program that can accept inputs and produce outputs. O A function can be called or executed by another program or function. O Code for a function is done in an Editor window or any text editor same way as script and saved as m-file. O The m-file must have the same name as the function. 2
- 3. Introduction… OThe convention for naming functions is the same as for variables. OIt is important that you give meaningful variable names to variables inside a function that you write, so that you and others can understand what the function does. OThe function M-file must be saved in your current directory (otherwise it must be in the path) 3
- 4. Why use functions? OFunctions provide reusable code OUse same code in more than one place in program without rewriting code OReuse code by calling in different programs OMake debugging easier 4
- 5. Before you start … OIdentify the function ODecide the function name ODecide the input variables ODecide the output variables OFile name must be the function name. 5
- 6. Function Syntax function[a, b, c]= basicmath(x,y) %BASICMATH Basic Mathematical function % basicmath(x,y) is a sample matlabfunction to perform % basic mathematical operations on input variables x & y % outputs of the function are sum, difference and product % of input arguments. a = x + y; b = x – y; c = x * y; 6 Output arguments Executable code Comments about the function Name of the function Input arguments declaration statement
- 7. Syntax … O The declaration statement function is used to define the file as a function. O It must be typed in lower case letters. O Input arguments O Typed in side the parentheses ( ) O Used to transfer data into function from calling program. O Can be zero or more input arguments. O Output arguments O Types inside the square brackets [ ] O Used to transfer data out of function to calling program. O Can be zero or more output arguments. O Give a meaningful variable name. O Rules for giving function name is same as the rules for variable names. 7
- 8. More on Function Arguments O Functions have private workspaces O Variables that are created inside of a user-defined function are referred to as local variables. O They can only be accessed from inside of that function. O After the function completes its operations, the local variables are deleted from memory. O The only variable that appears in the workspace is the output of the function. O Conversely, functions cannot access variables from the workspace. (with the exception of any input parameters they might have or “global variables”---see MATLAB help on this matter).

No public clipboards found for this slide

Be the first to comment