Creating Loops
    

Looping through commands

Suppose you want to train your eye to judge normal probability plots. So you decide to generate 20 plots for data from a normal distribution. First store the following commands in a file called NPLOT.MTB:

RANDOM 50 C1

NSCORES C1 C2

NAME C1 'Data' C2 'Nscores'

PLOT C1*C2

To execute this file 20 times, to get 20 different normal probability plots, type

EXECUTE "NPLOT" 20

You can also loop through rows of data. Suppose we have a full year of the laboratory data from our first example, one month stacked on top of another, in a file called LAB.DAT. There are now four variables, Yield, Chem1, Chem2, and Month. To do the same analysis as before, separately for each month, we store the following commands in the file YEAR.MTB:

NAME C11 'Yield' C12 'Chem1' C13 'Chem2' C20 'Resids' C21 'Fits'

COPY C1-C3 C11-C13;

  INCLUDE;

    WHERE "C4 = K1".

PRINT K1

DESCRIBE C11-C13

PLOT C11*C13

PLOT C11*C13

REGRESS C11 2 C13 C13 C20 C21

PLOT C20*C21

ADD K1 1 K1

Then, to analyze the file LAB, we type

LET K1 = 1

READ "LAB" C1-C4

EXECUTE "YEAR" 13

Looping through columns and matrices

A special feature, sometimes called the CK capability, allows you to loop through columns of the worksheet. Suppose you have a file, MYDATA.DAT, containing 21 variables and you want to plot the last variable versus each of the first twenty variables. That's twenty separate plots. First store the following commands in a file called PLOTS.MTB:

PLOT C21*CK1

ADD K1 1 K1

Then type

READ "MYDATA" C1-C21

LET K1 = 1

EXECUTE "PLOTS" 20

The first time through the loop, K1 = 1. This value is substituted for the K1 in the PLOT command, giving PLOT C21*C1. The next time through the loop, K1 = 2, giving PLOT C21*C2, and so on.

Matrices also have this capability, using MK1. Stored constants do not.

The next example shows how to accumulate column statistics in one column. Suppose you have data in C1 through C30 and you want to compute the mean of each column and store those means in C40. Store the following commands in the file MEAN.MTB:

LET C40(K1) = MEAN (CK1)

ADD K1 1 K1

Then type

LET K1 = 1

EXECUTE "MEAN" 30

The first time through the loop K1 = 1, so row 1 of C40 will equal the mean of C1. The next time through the loop K1 = 2, so row 2 of C40 will equal the mean of C2, and so on.