Matlab Tutorial 7: Common Programming Structures and Conditional Statements

Example 5:

In this example we will try if a dice has an odd or an even number. We will write a function m-file to decide this.

for variable=expression
control statements

Try your m-file with: dice(3), dice(6) and dice(8) !

Interrupt commands in MATLAB

In some cases we must have the option to interrupt the execution of the m-file. This could be due to error in keyboard input or some other obvious wrong calculation result or maybe just a need to pause the execution.
error(‘string’) Interrupts an m-file execution and writes the message ‘string’ in the command window.
Pause Pauses the execution and waits for a keyboard input or a timeout.
Break Interrupts while- and for-loops. If we have nested loops it only breaks the closest one.
In the examples below I will try to examplify how error and break can be used.

for i=1:10
fprintf('The sum of 10 elements gives: %f ', the_sum)

Save the m-file! We will use it later.

% M-filen random.m
% Examplify how break command works.
while t<inf
number=input('Give a number ! (-1 to stop) ');
if number==-1

Nested loops in MATLAB

Nested for and while-loops can occur in Matlab. The structure is shown below. Suppose we have created a matrix A=[1 2 3; 4 5 6]. Now we would like to write the elements row-wise from the matrix A.

for k=1:2 % row index
for j=1:3 % column index

Try it!
I do not think it will surprise you. Try also to change the program so the elements in the matrix A are written column-wise.
The structure for a nested while loop looks like:

while logical expression 1
statements 1
while logical expression 2
statements 2
statements 3

Try-catch-end in MATLAB

A try catch end statement is useful if there is any possibility of that an error might occur. The statements are executed in order and starting from the top. If there an error occurs in one of the statements1 the program is not terminated. Instead the error message is stored in ‘lasterr’ and the program continues with statements 2.
The syntax is:

lasterr % Gives the last error message in Matlab.

Where we want to divide two numbers with each other, but we still want the program to continue. No matter if we enter a letter or any non-number character.

Example 6:

Read 2 numbers and divide them to each other!

Q=input('Give the numerator : ');
P=input('Give the denominator: ');

What happens if we enter two numbers? Try to enter a letter or some other character!

Debuggingin MATLAB

No living human being writes programs without any errors. It could be simple syntactic errors or more complicated ones as logical errors. Matlab is equipped with a debugger to help the programmer to find the error. The simplest form of debugging is to put % ( makes it a comment) in front of the command line and then step by step add one command line at a time and see if the program works as intended. Maybe try with certain data that gives answers that you know are correct.
Nevertheless how extensive tests we make of our program. We can not claim with 100% certainty that the program is accurate, unless it is a very small and simple program. The errors could be we are using small letters instead of capital letters, some variables are undefined or the variables have previous values that we have disregarded.
Common mistakes are a forgotten parenthesis or operator, but the most serious ones are the logical errors. The algorithm we are using are wrong and the program is executable and delivers wrong answers. These kind of errors can be very tough to detect.
Now let us move on and see how we can use the debugger. Put the program below in the editor. In the edit window you can easily find the symbol button for the debugger (white sheet with a red dot).
Click on every command line in your m-file!
Now there will be a red dot on every single line. This is a breakpoint.
Whenever you run the program in debugging mode the program will stop at every breakpoint it will find in the program.
In our case it will stop at every line.

% M-filen nest.m
A=[ 1 2 3; 4 5 6] % We have a 2-dimensional matrix here.
for k=1:2 % row index
for j=1:3 % column index
disp(A(k,j)) % display the matrix element

In the command window you can notice that the prompter is changed from >> to K>>. Run the m-file and go from one breakpoint to another by the F10 button in the command window. The result is shown in appendix A. Notice also how it looks in the edit window. There is a green arrow indicating what line that is executed at the present. By placing the mouse cursor over a variable name the value for each is shown.
Whenever you want to leave the debugging mode just click on the same button that started the debugger.

Pages: 1 2 3 4

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment