Documente Academic
Documente Profesional
Documente Cultură
Halmstad University
School of Information Science, Computer and Electrical Engineering
Laboratory 8
Length about 2 hours with supervision.
Regardless if you completed the whole Lab-PM during these 2 hours you should try
to find the time to work through this Lab-PM thoroughly afterward.
The last page contains hand-in assignments. You should try to solve them within a
week to be able to follow the pace in the course.
Everything that follows after >> is written in the Matlab Command Window and
indicate what you as a user should write. The % is intended as a comment to a
specific command.
In every assignment we will face problems that need to be solved and then we use m-files
to solve them for us, but if we stand before more complex problems, it is often a better
strategy to divide the problem in smaller parts. One of the parts could be the main
program ( m-file )and other could be focused in problem solving. These are so called
function m-files.
In many programming languages this is nothing else than subroutines.
Communication between the function m-files is performed with input or output
parameters. A function m-file can have 0,1 or several input and output parameters.
The same function can also be called with a different number of input arguments from
time to time.
Like the m-file plot in Matlab.
nargin
A function that returns the number
arguments used in the function call.
nargout
Returns the number of output arguments
in a function call.
inputname(x)
Gives the name of the variable at position
x in the function call. Must be within the
function m-file to be used.
Error_string=nargchk(min,max,number)
Checks the number of input arguments to a
function. Min and max gives the smallest
and largest number of input arguments.
Number gives the actual number of
parameters. If number lies outside the
interval, then Error_string is given.
Error_string=nargoutchk(min,max,number) Checks the number of output arguments to
a function. Min and max gives the smallest
and largest number of output arguments.
Number gives the actual number of
parameters. If number lies outside the
interval, then Error_string is given.
function [varargout]=statistics(varargin)
for i=1:length(varargin)
x=varargin{i};
y.mean_val=mean(x);
% Stores the result in a structure y.
y.median_val=median(x);
y.std_val=std(x);
varargout{i}=y;
% The structure y is put in a output vector.
end
Let us test our function file by calling it, but first we create some vectors of variable
length.
>> a=1:4; b=2:10; c=3:5;
>> fh=@statistics;
Create a vector X=[ 1 2 3 4 5 6];
Evaluate the function in X with:
>> feval(fh,X)
ans =
mean_val: 3.5000
median_val: 3.500
std_val: 1.8708
Here comes yet another example of how we can use handles, but in this case you might
recognize the example. Look upon it as an repetition example.
>> feval(statistics,X)
----------------------------------function y=func(x)
y=x.^2;
---------------------------------Run the file from the command window, but first make a function handle.
>> Fh=@func;
>> func
What function do you want ? : log10
% I chose 10-logarithm.
x
y
1.0000
0
2.0000 0.3010
3.0000 0.4771
4.0000 0.6021
Range of a variable
A function m-file containing variables can only rely on that these are known within this
function. As soon as the execution of the function m-file is finished the variables will be
forgotten.
If you want the variable to keep its value to the next execution you must make a
declaration and use the word persistent.
persistent variable_1
Even with the declaration in the function m-file above, the variable remain to be
unknown outside the m-file. If you want to make variable_1 known in the workspace or
in other m-files, it must be declared as a global variable in all these m-files.
global variable_1
Note that a variable_1 existing in an m-file fun and not globally declared is not equal to
a variable with the same name, used in workspace.
The variables may have the same name, but are nevertheless unknown to each other.
To examplify this. Create a function m-file fun.
---------------------------------------------------------------function y=fun(x)
global a_ b_
y=a_+b_*x;
----------------------------------------------------------------Then create an m-file plot_fun that invokes the function m-file fun.
The variables a_ and b_ are global and must be declared in both m-files.
Our main program will then look like:
--------------------------------------% plot_fun.m
global a_ b_
x=0:0.1:10;
y=fun(x);
% Call of the function fun with input parameter x.
plot(x,y),grid
title(The function y= a+b*x)
-------------------------------------Call the main program plot_fun. To see the result look in figure 1 !
20
15
10
10
Figure 1
Clear the m-file fun from our work directory and run our m-file plot_fun once again.
The output from the execution is the same as previously.
Try also:
>> type fun % Show fun.p !
No information about the p-file is displayed.
M-file control
With the command echo you can echo the command lines of an m-file during execution.
This could be useful in order to view the command lines as we execute. For instance for
debugging or demonstration purposes.
pause
Execution of an m-file is paused
until a keyboard button is pressed.
pause(n)
Stops the execution for n seconds
and then continues.
echo function_name on
Turns echo on in function_name.m
echo function_name off Turns echo off in function_name.m
return
Return to invoking function it also
terminates the keyboard mode.
keyboard
Stops execution of the file
and gives control to the user's
keyboard. The special status is
indicated by a K appearing before
the prompt.
To leave this mode, write return
after the prompt.
error(message)
Displays the error message in the
string MESSAGE and exits from the
m-file.
break
Interrupts for- and while-loops, but
if these are nested only the
innermost loop.
We will now try to illustrate the command keyboard. Modify the m-filen plot_fun.
--------------------------------------% plot_fun.m
global a_ b_
a_=1;
b_=2;
keyboard
% PLEASE NOTICE !
x=0:0.1:10;
y=fun(x);
% Call of the function fun with input parameter x.
plot(x,y),grid
title('The function y= a+b*x')
-------------------------------------When I try to run the program. The control of the m-file is given to the keyboard.
Write !
>> echo on % Turns on echoing of commands inside m-files.
>> plot_fun
global a_ b_
a_=1;
b_=2;
keyboard
K>>
% Everything is now controlled from the keyboard.
Change the values of the global parameters !
K>> a_=0,b_=10
a_ =
0
b_ =
10
Continue the execution. Write return in the command window after the prompter K>> !
x=0:0.1:10;
y=fun(x);
% Invoke the function fun with parameter x.
plot(x,y),grid
title('The function y= a+b*x')
The function y= a+b*x
100
90
80
70
60
50
40
30
20
10
10
Figure 2
In the figure 2 we can see the result of the m-file plot_fun execution.
Write !
>> echo off
PLEASE NOTICE !
THE ENGLISH VERSION OF LABORATORY 8 IS SOMEWHAT SHORTER THAN
THE SWEDISH VERSION, BUT THE MISSING 3 PAGES ARE NOT NEEDED TO
DO THE HOME ASSIGNMENTS.
I WILL TRY TO TRANSLATE THESE AS SOON AS POSSIBLE !
8