Adding a FOR-LOOP to Your Macro

 

You certainly can repeat recording one curve fit after another to create a macro that performs multiple curve fits but this restricts you to using this macro on exactly that number of curve fits and produces lots of repetitive macro code. A better procedure is to modify your macro code by placing a for-loop around the macro code recorded for the first curve fit.

To do this, open your macro for editing by selecting Tools, Macro, Macros…, Exponential Fit, Edit. The macro edit window is displayed. Expand the window vertically as much as possible and scroll down through the code. Near the beginning of the macro you will see

SUB MAIN
ACTIVEDOCUMENT.CURRENTPAGEITEM.SELECT(FALSE, -1688, 1521, -1688, 1521) 
ACTIVEDOCUMENT.CURRENTPAGEITEM.SELECT(FALSE, -1688, 1521, -1688, 1521)

This is the beginning of the main subroutine and the first two “Select” statements that correspond to your right click on the graph data point that was the first recorded operation in your macro. The numbers in this statement refer to the position on the graph page where you clicked. Scroll down about 35 more lines to

SET FITFILE = NOTHING
ACTIVEDOCUMENT.CURRENTPAGEITEM.SELECT(FALSE, -1667, 958, -1667, 958) 
ACTIVEDOCUMENT.CURRENTPAGEITEM.SELECT(FALSE, -1667, 958, -1667, 958)

The Set FitFile = Nothing statement is the last statement of the first recorded curve fit. The two “Select” statements are the start of the second curve fit (the right click on the data point for the second data set).

We are going to 1) delete all the statements recorded for the second curve fit, 2) place a for-loop around the first set of curve fit statements and 3) modify one line in the curve fit code to select a different y data column each time through the for-loop. To delete the statements for the second curve fit, highlight all lines starting with the first “Select” statement in the middle of the code to the last Set FitFile = Nothing statement just above the End Sub statement. Then delete these lines.

Now add the for-loop statements at the beginning and end of the remaining macro code. Enter the following two lines at the beginning of the macro

SUB MAIN
DIM I AS INTEGER
FOR I = 1 TO 2
ACTIVEDOCUMENT.CURRENTPAGEITEM.SELECT(FALSE, -1688, 1521, -1688, 1521)
ACTIVEDOCUMENT.CURRENTPAGEITEM.SELECT(FALSE, -1688, 1521, -1688, 1521)

and the “Next i” line at the end of the macro

SET FITFILE = NOTHING 
NEXT I 
END SUB

If you ran the macro now it would run twice and fit the data in columns 1 and 2 twice. So we need to change one more line in the code. Find the two lines that define the columns to fit

FITOBJECT.VARIABLE("X") = "COL(1)" 
FITOBJECT.VARIABLE("Y") = "COL(3)"

and modify the second line to be

FITOBJECT.VARIABLE("Y") = "COL(" + CSTR(I+1) + ")"

As the for-loop index i increments from 1 to 2 this statement changes the y column to be fit from “col(2)” to “col(3)”.

Lets test the modified macro. Click the Main button, Save to save these modifications and close the macro window. Make sure that the graph in the Exponential Data Set 1.jnb notebook is open (this is important since the macro was recorded starting with an opne graph). If the graph has fit lines from a previous run, then close the notebook without saving and reopen it. To run the macro, select Macros, Exponential Fit from the main menu. The macro runs, exponential functions are fit to both data sets and fit lines are displayed on the graph.

If you run the macro “Exponential Fit Example” in Exponential Data Set 3.jnb you will need to change the path to standard.jfl from

Path = “C:\Users\[user]\Documents\SigmaPlot\SPW12\Standard.jfl”

to your own profile path.

Since the macro placed curve fit results in the worksheet as its last step, the worksheet is displayed on top of the graph page. You will have to click on the graph window to make it the currently displayed window. When we write a macro to display curve fit lines on a graph we would like to have the graph displayed as the last step. We can easily modify our macro to bring the graph page to the front.

Try SigmaPlot FREE for 30 Days!