3.1.3. FUNCTION USED BY
MATLAB.
MATLAB standards functions are so many but we need this:
USER DEFINED FUNCTION.
1. graythresh function: is the function used to
determine a threshold value.
2. strel function: is a function used to
disappear the linear gaps if the sobel image is dilated using linear
structuring elements.
3. imdilate function: is the function used to
dilate the binary gradient mask using the vertical structuring element followed
by the horizontal structuring element.
4. imfill function: is the function used to fill
the interior holes(gaps) of image.
5. imclearboarder function: is used to remove
the connected object to the border of the image. We create the diamond
structuring element using the strel function.
6. bwperim function: is the function used to
place an outline around the segmented object.
7. BW = edge (I,'sobel'): specifies the Sobel
method.
8. BW = edge (I,'sobel',thresh): specifies
the sensitivity threshold for the Sobel method edge ignores all edges that are
not stronger than thresh. If you do not specify thresh, or if thresh is empty
([]), edge chooses the value automatically.
MATLAB STANDARD FUNCTION.
MATLAB is a powerful programming language as well as an
interactive computational environment. Files that contain code in the MATLAB
language are called M-files. You create M-files using a text editor, then use
them as you would any other MATLAB function or command.
There are two kinds of M-files:
Scripts, which do not accept input arguments
or return output arguments. They operate on data in the workspace.
Functions, which can accept input arguments
and return output arguments. Internal variables are local to the function.
If you're a new MATLAB programmer, just create the M-files
that you want to try out in the current directory. As you develop more of your
own M-files, you will want to organize them into other directories and personal
toolboxes that you can add to your MATLAB search path.
M-FILE SCRIPTS.
SCRIPTS are the simplest kind
of M-file because they have no input or output arguments. They are useful for
automating series of MATLAB commands, such as computations that you have to
perform repeatedly from the command line.
THE BASE WORKSPACE
Scripts share the base workspace with your interactive MATLAB
session and with other scripts. They operate on existing data in the workspace,
or they can create new data on which to operate. Any variables that scripts
create remain in the workspace after the script finishes so you can use them
for further computations. You should be aware, though, that running a script
can unintentionally overwrite data stored in the base workspace by commands
entered at the MATLAB command prompt.
Simple script example
These statements calculate rho for several trigonometric
functions of theta, then create a series of polar plots:
% An M-file script to produce % Comment lines
% "flower petal" plots
theta =
-pi:0.01:pi; % Computations
rho(1,:) = 2 * sin(5 * theta) .^ 2;
rho(2,:) = cos(10 * theta) .^ 3;
rho(3,:) = sin(theta) .^ 2;
rho(4,:) = 5 * cos(3.5 * theta) .^ 3;
for k = 1:4
polar(theta, rho(k,:)) %
Graphics output
pause
end
Try entering these commands in an M-file called petals.m. This
file is now a MATLAB script. Typing petals at the MATLAB command line executes the statements in the script.
M-FILE FUNCTION
FUNCTIONS are program routines, usually implemented in
M-files, that accept input arguments and return output arguments. They operate
on variables within their own workspace. This workspace is separate from the
workspace you access at the MATLAB command prompt.
THE FUNCTION WORKSPACE
Each M-file function has an area of memory, separate from the MATLAB base workspace, in
which it operates. This area, called the function workspace, gives each
function its own workspace context.
While using MATLAB, the only variables you can access are
those in the calling context, be it the base workspace or that of another
function. The variables that you pass to a function must be in the calling
context, and the function returns its output arguments to the calling workspace
context. You can, however, define variables as global variables explicitly,
allowing more than one workspace context to access them. You can also evaluate
any MATLAB statement using variables from either the base workspace or the
workspace of the calling function using the evalin function.
Simple function
example
The average function is a simple M-file that calculates the
average of the elements in a vector:
function y = average(x)
% AVERAGE Mean of vector elements.
% AVERAGE(X), where X is a vector, is the mean of vector
% elements. Nonvector input results in an error.
[m,n] = size(x);
if (~((m == 1) | (n == 1)) | (m == 1 & n == 1))
error('Input must be a vector')
end
y = sum(x)/length(x); % Actual computation
Try entering these commands in an
M-file called average.m. The average function accepts a single input argument
and returns a single output argument. To call the average function, enter
z = 1:99;
average(z)
ans =
50
TYPES OF M-FILES FUNCTION
Many functions are used. For instance, the following are the used
functions:
The Primaty M-File Functions: is the first function in an
M-file and typically contains the main program. Primary functios have a wide
scope, the primary function can be invoked from outside of their M-File(from
the matlab command line or from function in other file).
Subfunctions: act as subroutines to the main
function. You can also use them to define multiple functions within a single
M-file.
Nested Functions: are functions defined within another
function. They can help to improve the readability of your program and also
give you more flexible access to variables in the M-file.
Overloaded functions: are useful when you need to create a
function that responds to different types of inputs accordingly. They are
similar to overloaded functions in any object-oriented language.
|