You are on page 1of 6

Introduction to M- function programming

M-files:

M-files in MATLAB can be scripts that simply execute a series of


MATLAB statements or functions that can accept arguments and produce one
or more outputs.
M-files can be created using a text editor and are stored with a name of the form
filename.m such
as average.m, filter.m etc.

To create M-file:
File menu New- M-file It will open M-file editor

The components of a function M-file are:


1. The function definition line
2. The H1 line
3. Help text
4. The function body

5. Comments

1. The function definition line:


The function definition line has the form:

function[outputs]= function_name(inputs)
end

Here, the output arguments are enclosed by square brackets and the
inputs are enclosed by
parentheses.
If the function has a single output statement, list the argument without brackets.
If the function has no output, only the word function is used, without brackets or
equal sign.
Functions can be called at the command prompt as follows:

2
>>[s,p]=sumprod(f,g)

2. The H1 line

The H1 line is the first line. It is a single comment line that follows the function
definition use.
There can be no blank lines or leading spaces between the H1 line and the
function definition
line.

Example:
% sumprod computes the sum and product of two images.

3. Help text

4. The function body

5. Comments

1. Function with no return value and no arguments

function myfunction()
function body
end

OR

function[]=myfunction()
function body
end

Example:
function myfunction()
disp(‘hello’);
end

At command prompt:
>> myfunction()

3
Hello

2. Function with arguments and no return value

function function_name(x)
function body
end

or

function []=function_name(x)
function body
end

Ex. function myfunction(x)

c=x*x;
disp(c);
end

At command prompt:

>>x=5
>>myfunction(x)
x=25

3. Function with return type and arguments

function [c]=myfunction(x)
c=x*x;
end

At command prompt:

>>a=5
>>myfunction(a)
c=25

4
Interactive I/O

MATLAB provides following I/O functions for displaying information to users


and accepts input
from the keyboard.

ii) disp()

ii) input()

Function disp

The disp function is used to display information on the screen.


If argument is an array, disp displays its contents.
If argument is a text string then disp displays the characters in the string.

Example:

>>A=[1 2;3 4];


>>disp(A)
ans=

1 2
3 4

Example:

>>str=‘Digital image processing’;


>>disp(str)
Digital image processing

Example:

>>str=‘Digital image processing’;

>>disp(‘This is another way to display text’) This is another way to display text

5
Function input

The input function is used for inputting data or for accepting an input from
keyboard.

Syntax:
t=input(‘message’);

This function outputs the words contained in message and waits for an input
from the user
and stores the input in t.
This input can be a single number, a character string, a vector (enclosed by
square brackets)
and elements separated by spaces and commas, a matrix or any other valid
MATLAB data
structure.

Example:

>>a=input(‘enter a=‘)
enter a=5
>>size(a)
ans=
1 1
2
Example:

>>t=input(‘Enter your data=‘,’s’)


Enter your data=1,2,4

t=
124

>>size(t)

ans=

1 5

6
Example of input() and disp()

%M-file for disp() and input()


a=input(‘enter a=‘);
b=input(‘enter b=‘);
disp(a+b)

Output:
enter a=5

enter b=5

10

You might also like