11.2.3 Adding New Origin C Functions to Origin
Functions written in Origin C are accessible from various locations within the Origin interface, such as the Script Window, provided they meet the following criteria: the function should return either void (as in the previous tutorial), double, string, or vectors of type double or string. Variables passed to the function from Origin should be of type double or string, or vectors of these types. Functions that do not meet these criteria are not callable from the Origin interface, but can be called within other Origin C functions. Note that although an Origin C function that accepts and returns type int can be called from the Origin interface, the data may be truncated since the interface only supports type double.
In this tutorial, you will be introduced to writing a math function that returns computed values. We will first create a function that returns type double to Origin, and then we will create a function that returns vectors of type double.
Minimum Origin Version Required: Origin 8.0 SR0
What you will learn
How to add a new function and how to run this function in the Script Window.
- Start a new Origin C file in Code Builder.
- Enter the following code:
double myfunc1(double x, double a)
return sin( a * x );
- Click the Build button to compile the function.
This function can now be called from the Origin interface, in places such as the Script Window.
- Go to the Script Window, and type in the following lines, pressing ENTER at the end of each line:
y = myfunc1(2, 3)
You can also use worksheet cells instead of absolute numbers:
- Make a worksheet active, enter a number in the first row of column A. Then type the following into the Script Window and press ENTER:
col(B) = myfunc1(col(A), 3)
Note that a function such as myfunc1, that accepts and returns type double, can also be used to perform vector operations.
- Fill rows 1 through 10 of Column A with numbers, and type the following into the Script Window:
col(B) = myfunc1(col(A), 3)
In the above example, Origin calls the myfunc1 function for each row of column A. For performing vector operations as above, it is more efficient to write functions that accept and return vectors.
- Go back to Code Builder and add the following function to the same file, and compile the file by clicking the Build button
vector<double> myfunc2(vector<double> vecIn, double a)
vecOut = sin( a * vecIn );
- Go back to the Origin interface, fill Column A with some new numbers, and type the following into the Script Window:
col(B) = myfunc2(col(A), 3)
The function myfunc2 is called only once for computing the entire column.
Note that you can use such functions in other places such as the "Set Column Values" dialog. The Auto Update feature of "Set Column Values" can be enabled by checking the appropriate check box in this dialog. As long as the Origin C function is compiled and ready to be called from Origin, any changes to the source column will result in an update of the destination column.