4.1 FAQ-286 How do I define global functions, global variables, and constants?
Last Update: 3/17/2017
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
- If Origin is running, exit the program.
- Start a fresh page in a text editor (e.g. Notepad) and enter the following information:
const constName = value; // e.g. Avogadro's number, Boltzmann constant, etc.
datatype varName = value; // insert your variables here, datatype (int, double, etc.) is optional
- Name and save the file to your User Files Folder (UFF), using an .OGS file extension (e.g. myvar.ogs).
- Browse to your Origin.ini file in your UFF and open this file in your text editor.
- Locate the [Startup] section of the file (if you do not have such a section, then create it) and add the name of the .OGS file you just created using the following form:
- Save your changes to Origin.ini.
- 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.
Note: You may see a reference to orgvar.ogs, in the [Startup] section of Origin.ini. This file, also found in your UFF, can be used to store constants and variables but we do not recommend using it. A better solution is to create your own .OGS file, and define your constants and variables in that file, as described above.
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:
Open Script Window using the menu Window: Script Window
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:
- 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
- Add following codes in the AfterOpenDoc section, and save the file and save the project
- 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
- Start a new Project and use View: Code Builder menu item to open Code Builder.
- 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.
- Under the [AfterOpenDoc] section, add the following lines of code:
double myconst = 20;
- Save the file and close Code Builder.
- 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.
- Open the just saved project again. This will trigger the [AfterOpenDoc] section to be executed and thus myconst to be defined.
- Activate a worksheet and import the file Samples\Signal Processing\fftfilter1.DAT.
- Add one more columns to the worksheet.
- Click on the header of the 3rd column to select it, then right-click and select Set Column Values... from the context menu.
- In the Set Values dialog that opens, enter the following in the edit box:
- Now save the Project again and re-open it. Choose Window: Script Window from Origin menu
- 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
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:
- 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.
- 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