4.1 FAQ-286 How do I define global functions, global variables, and constants?

Last Update: 11/18/2024

Global Functions for use across Origin sessions

To define a global function for use across Origin sessions, view this page.

For further information on scope of a function, refer to this page.

Global Constants and Variables for use across Origin sessions

When Origin starts, a text file in the user file folder named CONST.CNF is read. If you have mathematical constants that you regularly use in your work, the CONST.CNF file is a good place to define them, as they will always be available when Origin runs

  1. If Origin is running, exit the program.
  2. Open the CONST.CNF file under User File Folder or start a fresh page in a text editor (e.g. Notepad) and enter the following information:
  3. const e = 2.718281828459045 //Euler's number
  4. Name CONST.CNF and save the file to your User Files Folder (UFF).
  5. To check your constant and variable values, run Origin, open the Script Window and type the name of the constant or variable, followed by "=" and press ENTER.

Constants and Variables in the current Origin Session

To define constants and variables for the current session of Origin, you can simply define them using the Script Window:

  1. Open Script Window using the menu Window: Script Window
  2. Type desired constant or variable assignment and press Enter at end of each line, such as:
    const myconst = 1.234;
    double myvar = 12.34;
    int mynum = 127;
    

Then any subsequent calculations in the script window, or any other location where script can be executed such as Set Column Values dialog, these constants and variables can be accessed.

Constants and Variables specific to an Origin Project

To define constants and variables specific to a project, you can define these in the ProjectEvents.OGS file attached to the project:

  1. Open Code Builder from the View: Code Builder menu, and in the left panel, expand the Project tree and double-click to open ProjectEvents.ogs
  2. Add following codes in the AfterOpenDoc section, and save the file and save the project
    • @global=1;
    • Define your variables and constants

These definitions will then be local to the project.

The following mini tutorial shows how to add a user-defined variable at the Origin project level and then use that variable

  1. Start a new Project and use View: Code Builder menu item to open Code Builder.
  2. Expand the Project branch on the left panel tree and double-click to open the ProjectEvents.OGS file. This file exists by default in any new Project.
  3. Under the [AfterOpenDoc] section, add the following lines of code:
    @global=1;
    double myconst = 20;
  4. Save the file and close Code Builder.
  5. In Origin, save the Project to a desired folder location. The OGS file is saved with the Project, so the user-defined variable is available for use in the Project.
  6. Open the just saved project again. This will trigger the [AfterOpenDoc] section to be executed and thus myconst to be defined.
  7. Activate a worksheet and import the file Samples\Signal Processing\fftfilter1.DAT.
  8. Add one more columns to the worksheet.
  9. Click on the header of the 3rd column to select it, then right-click and select Set Column Values... from the context menu.
  10. In the Set Values dialog that opens, enter the following in the edit box:
    wcol(2)*myconst
  11. Now save the Project again and re-open it. Choose Window: Script Window from Origin menu
  12. Enter script below in the Script Window and press Enter to change the values of myconst and trigger recalculation.
    myconst = 30;; //changes myconst value in this session only. Not saved in project
    run -auf 2 1; //trigger recalculation
  13. More details about run -auf command can be found here.

Variables specific to a Worksheet

If you need to define some variables specific to a worksheet, and then update the results in other columns by changing this variable, follow these steps:

  1. Add a couple of columns to the current sheet, or to a new sheet, and place the variable names in 1st column and the values in the 2nd column. The names are purely for reference.
  2. When using Set Values dialog to compute values for other columns, you can then use the Before Formula Script panel to define local variables in your script to point to specific cells of your variables column, and then include them in your column formula in the upper panel. If the recalculation mode is set to Auto or Manual, then you can simply change the variable values and have the calculated values update. See the help file section on Set Values dialog for more information.

Constants in a Fitting Function

The Variables and Parameters page of the Fitting Function Builder wizard provides an edit box for defining constants. The constants can then be assigned values in the Constants tab of the page where the function body and the parameters are defined.

For more details, please refer to scope of variables.



Keywords:variable, define variable, constants, functions, global variable, @glob, @global, project variable, ORGSYS.CNF, Set Values, Before Formula Script, ProjectEvents.OGS, Script Window