Professional Documents
Culture Documents
M-files:
To create M-file:
File menu New- M-file It will open M-file editor
5. Comments
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
5. Comments
function myfunction()
function body
end
OR
function[]=myfunction()
function body
end
Example:
function myfunction()
disp(‘hello’);
end
At command prompt:
>> myfunction()
3
Hello
function function_name(x)
function body
end
or
function []=function_name(x)
function body
end
c=x*x;
disp(c);
end
At command prompt:
>>x=5
>>myfunction(x)
x=25
function [c]=myfunction(x)
c=x*x;
end
At command prompt:
>>a=5
>>myfunction(a)
c=25
4
Interactive I/O
ii) disp()
ii) input()
Function disp
Example:
1 2
3 4
Example:
Example:
>>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=
124
>>size(t)
ans=
1 5
6
Example of input() and disp()
Output:
enter a=5
enter b=5
10