1 of 22

Numeric, cell arrays, matrix operations, user defined functions, programming using MATLAB; debugging MATLAB programs, applications to simulations

COM 103 ICT Applications in Food Industry (1+2)

Lecture Schedule 5 – Part - III

Dr. S. Sridevy, Ph.D.

Assistant Professor (Computer Science)

Dept. of Physical Sciences & IT

AEC&RI, TNAU, Coimbatore – 3.

22 June 2020

2 of 22

Variable Editor Window

  • Variable editor window can be opened
  • By double clicking the variable in the workspace or
  • Entering ‘var’ in command window

S. Sridevy

22 June 2020

3 of 22

S. Sridevy

22 June 2020

4 of 22

User Defined Functions

  • Another type of M- file is a function file.
  • Both scripts and functions allow to reuse sequences of commands by storing them in program files.
  • Scripts are the simplest type of program, since they store commands exactly as they are typed at the command line.

S. Sridevy

22 June 2020

5 of 22

User Defined Functions (Contd.)

  • Functions provide more flexibility, primarily because functions contain sequential commands and can accept inputs and return outputs.
  • Unlike a script file, all the variables in a function file are local variables, which means their values are available only within the function.

S. Sridevy

22 June 2020

6 of 22

User Defined Functions (Contd.)

  • Function files are useful when you need to repeat a set of commands several times.
  • They are the building blocks of larger programs.
  • To create a function file, open the Editor /Debugger window

S. Sridevy

22 June 2020

7 of 22

User Defined Functions (Contd.)

  • Named functions must be defined within a program file and not at the command line.
  • The general syntax for defining a function:

function [y1,...,yN] = myfun(x1,...,xM)

    • where y1,...,yN are outputs, x1,...,xM are inputs, and myfun is the function name.

S. Sridevy

22 June 2020

8 of 22

User Defined Functions (Contd.)

  • The first line in a function file must begin with a function definition line that has a list of inputs and outputs.
  • This line distinguishes a function M- file from a script M- file.
  • The body of a function can include valid MATLAB expressions, control flow statements, comments, blank lines, and nested functions.

S. Sridevy

22 June 2020

9 of 22

User Defined Functions (Contd.)

  • Any variables that is created within a function are stored within a workspace specific to that function, which is separate from the base workspace.
  • Functions end with either an end statement, the end of the file, or the definition line for a local function, whichever comes first.

S. Sridevy

22 June 2020

10 of 22

User Defined Functions (Contd.)

  • The end statement is required if:
    • Any function in the file contains a nested function
    • The function is a local function within a function file,
    • The function is a local function within a script file.
  • Although it is sometimes optional, use end for better code readability.

S. Sridevy

22 June 2020

11 of 22

Syntax Elaborated

S. Sridevy

22 June 2020

12 of 22

Function Keyword

  • function keyword (required)
  • Use lowercase characters for the keyword.

S. Sridevy

22 June 2020

13 of 22

Output arguments (optional)

  • If your function returns one output, you can specify the output name after the function keyword.

function myOutput = myFunction(x)

  • If your function returns more than one output, enclose the output names in square brackets.

function [one,two,three] = myFunction(x)

S. Sridevy

22 June 2020

14 of 22

Output arguments (optional)

  • If there is no output, you can omit it.

function myFunction(x)

  • Or you can use empty square brackets.

function [] = myFunction(x)

S. Sridevy

22 June 2020

15 of 22

Function name

  • Function name is required
  • Valid function names follow the same rules as variable names.
  • Function name must start with a letter, and can contain letters, digits, or underscores.

S. Sridevy

22 June 2020

16 of 22

Input arguments

  • Input arguments are optional
  • If your function accepts any inputs, enclose their names in parentheses after the function name. Separate inputs with commas.

function y = myFunction(one,two,three)

  • If there are no inputs, you can omit the parentheses.

S. Sridevy

22 June 2020

17 of 22

Example 1

  • The function named fact computes the factorial of a number (n) and returns the result (f).

function f = fact(n)

f = prod(1:n);

end

S. Sridevy

22 June 2020

18 of 22

Example 1 (Contd.)

  • Call the function from the command line

x = 5;

y = fact(5)

y =

120

S. Sridevy

22 June 2020

19 of 22

Example 1 (Contd.)

  • Call the function from the command line
  • x = 5; y = fact(5)
  • y = 120

S. Sridevy

22 June 2020

20 of 22

Example 2

  • Starting in R2016b, another option for storing functions is to include them at the end of a script file.
  • For instance, create a file named mystats.m with a few commands and two functions, fact and perm. The script calculates the permutation of (3,2).

S. Sridevy

22 June 2020

21 of 22

Example 2 (Contd.)

x = 3;

y = 2;

z = perm(x,y)

function p = perm(n,r)

p = fact(n)*fact(n-r);

end

function f = fact(n)

f = prod(1:n);

end

S. Sridevy

22 June 2020

22 of 22

Example 2 (Contd.)

  • Call the script from the command line.

mystats

z =

6

S. Sridevy

22 June 2020

Thank You!