5.2 Introduction to LabTalk
Contents
Summary
This lesson introduce basic LabTalk statement with examples.
"=" Statement
Quick Calculation
10+3*5=; //output 10+3*5=25
Expression (RHS, right-hand side) is evaluated and put into LHS (left-hand side). If LHS does not exist, it is created if possible, otherwise an error will be reported.
Assign value to variable and output values
//assign double min = 0.5; string filename$="sensor01.dat"; dataset ds={0.1, 0.2, 0.3}; range ra=[book1]sheet1!A; //range variable for column A in specified book and sheet ra=data(800, 1000, 10); //fill it with 800 to 1000 with increment of 10 //output min =; //output 0.5 filename$=; //output sensor01.dat ds[1]=; //output 0.1, 1st value of ds ra[2]=; //output 900, 2nd value in column A
Note: For dataset and range variable, need to use e.g. type $(ds); type $(ra); to output all values
Read and write object properties
wks.name$=; //output current sheet name. For string property, use $ at the end wks.name$="Data"; //set wks.ncols= ; //output number of columns in current sheet; wks.ncols=10; //set number of columns to 10 wks.=; //list all wks object properties Col(A)[1]=; //return first value in column 1 Col(A)[0]=; //return last value in column 1 wks.col1.=; //list all wks.col1 (1st column) object properties
Command
LabTalk command statements are similar to DOS batch commands in form of
command -option argument(s);
- Depending on the command, the option and argument may be optional.
- Options are always preceded by the dash "-" character.
- Arguments are either a script or a data object. In many cases, options can also take their own arguments.
The type command takes option (-n) and and argument (string to output)
//Display string in a dialog box with Yes and No buttons type -n "Do you want to continue?";
The repeat command takes two arguments the number of times to execute, and script to repeat
//Prints "Hello World" in a dialog box three times. repeat 3 {type -b "Hello World"}
Conditional and Loop Structures
LabTalk contains expressions, operators, and control flow keywords and structure similar to C, Such as if, if-else, switch, break, continue and for.
The following script checks current window (%H) type by Origin's exist() function.
if(exist(%H)==3) type -b "A graph is active"; else if(exist(%H)==2) type -b "A worksheet is active";
The following shows using for loop to set columns as XYXY... and their long names
wks.ncols=10; for (ii=1; ii<=wks.ncols; ii++) { if (mod(ii,2)==1) { wks.col$(ii).type=4; wks.col$(ii).lname$="Time"; } else { wks.col$(ii).lname$="Amplitude"; } }
There are also Origin's own loop commands e.g. loop and repeat to repeat scripts.
//loop through num from 1 to 4 and type the num value, everytime num plus 1 loop (num, 1, 4) {type "$(num)";} //create 6 new workbooks repeat 6 { newbook;}
Document command to loop through Origin objects (window, sheet, etc.)
The following script loops through all graph windows and set legend background to shadow and refresh the graph
doc -e LP { legend.background=2; //set legend background to shadow doc -uw; //refresh window }
Function
Origin provides many built-in LabTalk functions to work with different types of data and object in Origin, such as string, math, statistics, date and time, data generation, lookup, logic, etc. User can also define LabTalk functions and call them in Script.
Use the Left( ) function to process strings
//Define a string variable and assign "Hello World" to it string str1$ = "Hello World"; //Use 'Left' function to get the left 5 characters the string string strLeft$ = Left(str1$,5)$; strLeft$ = ; //should return Hello
Some functions can appear on the left side of an assignment, e.g. Col() function
//Fill column A with data() function col(A) = data(600,850,10); //data() function creates a dataset from 600 to 850 in steps of 10
Use mean(), min() and max() functions to calculate mean, min and max of column and output them by type command.
for (ii=1; ii<=wks.ncols; ii++) { type "Col $(ii): Mean -- $(mean(wcol(ii)), .2), Min -- $(min(wcol(ii)),.2), Max -- $(max(wcol(ii)),.2)"; }
LabTalk Object
There are many Origin LabTalk Object (visible or not visible) to control Origin and its elements. Object contains properties and methods (functions) accessible via LabTalk script.
E.g. run object isn't visible in workspace. Use it to run ogs file.
- run.file(filename) to run the script before any section at the beginning of the ogs file.
- run.section(filename, sectionname) to run the specified section in ogs file
See the example in Save Script as an OGS File and Run it section in previous [[LabTalk:Tutorial_Get_Started#Run_Script_From_OGS_File|Getting Started tutorial].
There are also many visible objects. Each workbook and graph window contains basic elements (objects). These objects each have pre-defined names and unique properties and methods that can be controlled using LabTalk. For a complete list of Origin objects, view the Help file.
E.g. each window has page object for the window. For graph window, there is also at least one layer object.
Have a graph window active and run the following to set window long name and anti-alising on so curves look smooth on screen
page.longname$="My Chart"; //set window long name page.aa=1; //set anti-alising on
There is at least one worksheet object wks in workbook. Each worksheet has column sub-objects (coln) for each (nth) column. Start a new worksheet and run the following to fill data, add column, set filter condition and apply the filter
//fill column A with 600 to 850 with increment of 10 col(A) = data(600,850,10); //fill column B with with random numbers, number of data is same as col(A) col(B) = normal(wks.col1.nrows); // nrows of wks.col1 object //Add a column wks.addcol(); //addcol() method of wks object wcol($(wks.ncols))=col(B)/col(A); //ncols property of wks object wks.col$(wks.ncols).type=3 //set last column as YError bar //Set filter condition in col2 wks.col2.filter$="x>0"; //filter$ property of wks.coln object //Apply the filter so only rows with positive value in col2 will show wks.runfilter(); //runfilter() method of wks object
Note: In above script both wks.col$(wks.ncols).type=3 and wks.col3.type=3 work but using the wks.ncols property is a better coding practice since it refers to last column. The $(wks.ncols) is a numeric substitution notation which means that the value of wks.ncols is evaluated first and then that value gets substituted before the LabTalk statement is executed.
X-Functions
Most of the dialogs and functions in Origin are based on X-Functions, which are callable from LabTalk For example, if you select the Analysis:Signal Processing:Smooth menu, a dialog will open allowing you to smooth irregular and noisy data. You can tell the dialog is built by X-Function since the dialog title shows Dialog name:X-Function name.
Not all X-Functions are associated with menu or toolbars so you may not be able to find all X-Functions from GUI. In such cases, the best approach is go to Help: Programming: X-Function and check the X-Function Reference with are organized by category.
The syntax to call X-Function is as follows, xFunctionName [argument1:=<range,name or value> argument2:=<range,name or value> ... ][ -switch];
where the arguments and -switches may be optional depending on the X-Function.
The following script uses [newbook x-function to create a new workbook with arguments to set book name and sheet number
//The workbook will be named as Test1 (both long and short name) //There'll be 5 worksheets in this workbook newbook name:=Test1 sheet:=5 option:=lsname;
X-Functions have a number of input and output variables. In this case, we specified name, sheet and option. For those we skipped, default value will be used.
Some useful switch options to find info. of the x-function
newbook -h; //dump help newbook -hv; //dump variable list newbook -he; //dump script example newbook -d; //open X-Function dialog
Macro
Associate a name with given script which can later be used as a command and include arguments.
Define a simple macro and use it. Run the following script first to define a macro named as testmacro
def testmacro { repeat %1 { repeat %2 { type -l "="; } type; } }
Use the macro
testmacro 2 64; // 2 is substituted for %1 and 64 for %2
Origin C Functions
A function properly implemented with Origin C can be called by LabTalk script.
- Open Code Builder and choose File: New...
- Choose C File and set File Name test and click OK.
- Start a new line at the bottom of the file and paste the following OC code
double CubeRoot(double x) { if(x < 0) return -10^(log10(abs(x))/3); else return 10^(log10(x)/3); }
- Choose Build: Rebuild All.
- In Script window, run the following script to call the CubeRoot() function.
double aa = CubeRoot(125); aa = ;//It should return aa=5 Col(A)=data(1,100); //fill column A with 1, 2, ... 100 col(B)=aa*CubeRoot(Col(A)); //set col(B) value by function and variable
System Variable
System variables are used to monitor or control Origin behavior. E.g. the @ASC system variable to check and change the number of megabytes needed to trigger auto save checking.
//Run the following script line to get the variable value @ASC = ; //Run the following script line to set the variable value to be 10 (MB) @ASC = 10;