User defined Functions in MATLAB Part 1

8
User Defined Functions in MATLAB Part 1 Shameer A Koya 1

Transcript of User defined Functions in MATLAB Part 1

Page 1: User defined Functions in MATLAB Part 1

1

User Defined Functions in MATLAB

Part 1

Shameer A Koya

Page 2: User defined Functions in MATLAB Part 1

IntroductionO User-defined functions are similar to the

MATLAB pre-defined functionsO 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

Page 3: User defined Functions in MATLAB Part 1

3

Introduction…OThe convention for naming functions is

the same as for variables.O It 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)

Page 4: User defined Functions in MATLAB Part 1

4

Why use functions?OFunctions provide reusable codeOUse same code in more than one place in program without rewriting code

OReuse code by calling in different programs

OMake debugging easier

Page 5: User defined Functions in MATLAB Part 1

5

Before you start …O Identify the functionODecide the function nameODecide the input variablesODecide the output variablesOFile name must be the function name.

Page 6: User defined Functions in MATLAB Part 1

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;

Output arguments

Executable code

Comments about the function

Name of the function

Input arguments

declaration statement

Page 7: User defined Functions in MATLAB Part 1

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 argumentsO 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.

Page 8: User defined Functions in MATLAB Part 1

More on Function Arguments

O Functions have private workspacesO 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).