Page 53 - Basics of MATLAB and Beyond
P. 53

A quick way to ‘comment out’ a slab of code in an m-file is to enclose
                               it between a while 0 and end statements. The enclosed code will never
                               be executed.

                               For
                               The basic form of a for loop is:
                                    for index = start:increment:stop
                                        statements
                                    end
                               You can omit the increment, in which case an increment of 1 is assumed.
                               The increment can be positive or negative. During the first pass through
                               the loop the index will have the value start . The index will be
                               increased by increment during each successive pass until the index
                               exceeds the value stop . The following example produces views of the
                               peaks function from many angles:
                               clf
                               colormap(gray)
                               plotnum = 1;
                               z = peaks(20);
                               for az = 0:10:350
                                 subplot(6,6,plotnum)
                                 surfl(z),shading flat
                                 view(az,30)
                                 axis tight
                                 axis off
                                 plotnum = plotnum + 1;
                               end
                               The index of a for loop can be a vector or a matrix. If it is a vector
                               the loop will be done as many times as the number of elements in the
                               vector, with the index taking successive values of the vector in each pass.
                               If the index is a matrix, the loop will be done as many times as there
                               are columns in the matrix, with the index taking successive columns of
                               the matrix in each pass. For example:
                               >> q = pascal(3)
                               q  =
                                    1     1      1
                                    1     2      3
                                    1     3      6
                               >> for i = q,i,end
                               i  =
                                    1
                                    1
                                    1


                               c   2000 by CRC Press LLC
   48   49   50   51   52   53   54   55   56   57   58