MATLAB and LABView - Chapter 5
MATLAB and LABView - Chapter 5
Chapter 5
User-Defined Functions
• MATLAB function should be place in file with the same name as the function,
and the extent “.m”.
• A function may be invoked by typing its name directly in the Command Window,
or by including it in a script file or another function.
5.1 Introduction to MATLAB functions
• Execution begins at the top of the function and ends when either a return
statement, and end statement, or the end of the function is reached.
• The initial comment lines in a function serve a special purpose.
• The first comment line after the function statement is called the H1 comment
line. It should always contain a one-line summary of the purpose of the
function. The special significance of this line is that it is searched and displayed
by the lookfor command.
• The remaining comment lines from the H1 line until the first blank line or first
executable statement are displayed by the help command. They should be
contain a brief summary of how to use the function.
a = 2; b = [6 4];
fprintf('Before sample: a = %f, b = %f %f\n',a,b);
out = sample(a,b);
fprintf('After sample: a = %f, b = %f %f\n',a,b);
fprintf('After sample: out = %f\n', out);
>> test_sample
Before sample: a = 2.000000, b = 6.000000 4.000000
In sample: a = 2.000000, b = 6.000000 4.000000
In sample: a = 10.000000, b = 60.000000 40.000000
After sample: a = 2.000000, b = 6.000000 4.000000
After sample: out = 70.000000
5.2 Variable passing in MATLAB
5.2 Variable passing in MATLAB
x = r * cos(theta * pi/180);
y = r * sin(theta * pi/180);
• Function inputname returns the name of the actual argument used when a
function is called.
name = inputname(argno);
• If the argument is a variable, then its name is returned. If the argument is an
expression, then this function will return an empty string.
function myfunc(x,y,z)
name = inputname(argno);
disp(['The second argument is named ' name]);
5.3 Optional arguments
function [mag, angle] = polar_value(x,y)
%POLAR_VALUE Converts (x,y) to (r,theta)
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
x
5.6 Subfunctions, private functions, and nested functions
• Subfunctions are often used to implement “utility” calculations for a main function.
function a = mean(v,n)
% Subfunction to calculate average.
a = sum(v)/n;
function m = median(v,n)
% Subfunction to calculate median.
w = sort(v);
if rem(n,2) == 1
m = w((n+1)/2);
else
m = (w(n/2)+ w(n/2+1))/2;
end
5.6 Subfunctions, private functions, and nested functions
• Private functions are functions that reside in subdirectories with the special name
private.
• Nested functions are functions that are defined entirely within the body of
anotherfunction, called the host function.
5.6 Subfunctions, private functions, and nested functions
function res = test_nested_1
% This is the top level function.
% Define some variables.
a = 1; b = 2; x = 0; y = 9;
% Display variables before call to fun1
fprintf('Before call to fun1:\n');
fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y);
% Call nested function fun1
x = fun1(x);
% Display variables after call to fun1
fprintf('\nAfter call to fun1:\n');
fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y);
% Declare a nested function
function res = fun1(y)
% Display variables at start of call to fun1
fprintf('\nAt start of call to fun1:\n');
fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y);
y = y + 5;
a = a + 1;
res = y;
% Display variables at end of call to fun1
fprintf('\nAt end of call to fun1:\n');
fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y);
end % function fun1
end % function test_nested_1