3.3.2.10 Define

The Define command is used in two different ways:

  • Defines a macro called macroName, and associates it with the given script. MacroName can then be used like a command, and invokes the given script.
  • (from Origin 2022) Defines a range variable.

Syntax:

Define a Macro:

define macroName { script}

Macros can take up to five arguments. Use the %1, %2, %3, etc., notation within the script to indicate that the macro expects one or more arguments. A macro can accept a number, string, variable, dataset, function, or script as an argument, but all arguments are passed as strings. These are similar to MS-DOS batch command arguments. If arguments are passed to a macro, the macro can report the number of arguments using the macro.nArg object property.

To see the script associated with a defined macro, use:

define macroName

Define a Range:

define option rangeName range

Options

Define a Range:

Option Scope Level of Named Range
n define a worksheet scope range variable
nb define a workbook scope range variable
np define a project scope range variable

Examples:

Define a Macro:

Example 1

The following script defines the autoexec macro to create a new Origin worksheet from the ORIGIN.OTW template.

def autoexec { window -T Data Origin; };

Example 2

The next script defines a macro that uses a loop to print a text string three times.

def hello 
{ 
      loop (ii, 1, 3) 
            { type "$(ii). Hello World"; };
};

Once the hello macro is defined, typing the word hello in the Script window results in the printout:

1. Hello World

2. Hello World

3. Hello World


Example 3

The following script defines a macro named abc that expects a single argument. The given argument is then multiplied by 2, and the result is printed.

def abc { type "$(%1 * 2)"; };

If you define this macro (previous example) and then type the following in the Script window:

abc 5

Origin responds: 10

You could modify the abc macro (previous example) to take two arguments:

def abc { type "$(%1 * %2)"; };

Now, if you type the following in the Script window:

abc 5 4

Origin responds: 20

Define a Range:

Example 1

// Prepare 4 column XYXY
def -n Sensor1 [Book1]Sheet1!col(B);
def -n Sensor2 [Book1]Sheet1!col(D);
plotxy iy:=((Sensor1),(Sensor2));

Example 2

newbook;
wks.ncols = 4;
col(A) = data(1,12);
col(B) = data(21,32,1);
def -n ref1 col(A)[2];
def -n myrange col(A);
csetvalue col:=col(C) formula:="ref1*B"; 
csetvalue col:=col(D) formula:="total(myrange)";