MATLAB Functions

After reading the MATLAB Functions topic, you will understand function structure, anonymous functions, and sub-functions in MATLAB.

In MATLAB, a large program divides into subprogram for performing a specific task and this subprogram is called function. The function is generally reusable.

Function Structure

When we write function structure generally having two parts as function call and function definition.

Function Definition

The function definition having the statement(s) which executes when function called and after that function returns back to calling function.

Example:

The function definition in MATLAB is given by

function y = half(n)
y = n/2;
end

Explanation:

  • The function keyword represents the beginning of the function definition.
  • half is the function name.
  • y is output.
  • n inside function name half(n), is the input argument.

Function Call

The function call really calls the function. When function is call, it passes any argument (parameters) to calling function.

Example:

The function half.m that we already created earlier and now we call it.

% calling function half.m file
d = half(4)

Explanation:

  • MATLAB look for a function file half.m. The input inside the parameters as 4 compare with the input specified as n in the function file half.m definition. Hence, the output is 2.

Function with multiple argument pass and return values

In this type of function, we can pass more than one arguments to calling function as well as can receive any data from the calling function. The example below will show the way of writing this type of function in MATLAB programming.

The function definition is given by

function y = func_name(x,z)
y = x + z;
end

When we call function func_name with x=3 and y=4, gives results as

>> d = func_name(2,4)
d =
6

we can also call function func_name with more than one values of each variable i.e. x=3 and z=4, along with x=4 and z=6 gives results as

>> d = func_name([2,4],[4,6])

d =

6    10

Explanation

  • The function keyword represents the beginning of the function definition.
  • The x,z inside brackets (i.e. ()) represent arguments value accept from the user will go to function’s body.
  • The func_name represent the function name.

Anonymous Functions

This type of function can be constructed either at MATLAB command window or in any M-file function or script. The general form of an anonymous function is

anony_name = @(arglist) expr

where:

  • anony_name is the name of the anonymous function
  • @ is the symbol.
  • arglist is a list of input arguments.
  • expr is any mathematical expression.

Example

Aim (1): To demonstrate anonymous function that gives half of a number.

Program (1):

% defining anonymous function
half = @(x) x./2;

half(4)

Output (1)

ans =
2

Sub-Functions

M-file can contain code for more than one function. The first function is the primary function and remaining functions are called sub-functions. Each sub-function should have its own function definition line.

Example

Aim (1): To demonstrate anonymous function that gives half of a number.

Program (1):

% primary function definition
function [dble tple]=numb(v)
dble=double(v);
tple=triple(v);

function w=double(x)
w=2*x;
end

function z=triple(x)
z= 3*x;
end
end

Output (1)

>> e=[2 3];
>> [a b]=numb(e)
a =
4     6
b =
6     9

Calling MATLAB built-in Functions

MATLAB having large numbers of built-in function and you can directly call these function using your intuition.

Example

Program (1): To demonstrate how to access standard functions without import keyword.

x=45;
y=tand(x)
z=cosd(x)

Output (1)

y =
1
z =
0.7071

Explanation

  • Here we consider two standard functions tand and cosd, which we can use directly in the program.

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *