Calling Origin C Functions in Labtalk Script

 

Argument compatability between LabTalk types and Origin C types

The following table show what kind of LabTalk object can be passed as argument to the different kinds of Origin C function paramenter.

Y = supported

/ = not supported

OC argument type \ Tabtalk argument type int literal int variable double literal double variable string literal string variable string register dataset grobject tree
int Y Y Y Y / / Y Y / /
int& / Y / Y / / / / / /
double Y Y Y Y / / Y Y / /
double& / Y / Y / / / / / /
string Y / Y / Y Y Y / / /
string& / / / / / Y / / / /
vector<int> / / / / / / / Y / /
vector<int>& / / / / / / / / / /
vector<double> / / / / / / / Y / /
vector<double>& / / / / / / / / / /
vector<string> / / / / / / / Y / /
vector<string>& / / / / / / / / / /
vector<complex> / / / / / / / Y / /
vector<complex>& / / / / / / / / / /

 

Examples of Calling Origin C Functions From LabTalk

This section provides examples on how to call Origin C functions in LabTalk script or directly run in Script Window. Assume that the OC function is saved in MyFunction.c and be included in current workspace.

Pass int by Value

In LabTalk script, we support passing the different types of argument to OC function as an Integer. But be careful when pass double type, since it will be trimmed as an variable of Integer type, say 2.55 will be treated as 2; and when pass a string register to it, make sure that the content in the register can be converted to an Integer, if can not, will be treated as 0. The following code show how the OC function is defined and used in LabTalk script.

OC Function

void show_square(int a)
{
        printf("%d * %d = %d\n", a, a, a * a);
}

Calling in LabTalk Script or Command Window

[UserScript]
        
%A = 5; // string register
show_square(%A);
        
show_square(3); // const integer
        
int r = 2; // integer variable
show_square(r); 
        
double dd = 3.2; // double variable
show_square(dd);
        
show_square(5.5); // const double
        
dataset ds = data(3, 6, 1);
show_square(ds); //only the first item will be pass to OC function, that is 3.

Pass int by Reference

OC Function

int increment(int& a)
{
        int nOld = a;
        a++;
        return nOld;
}

Calling in LabTalk Script or Command Window

[UserScript]
                
int r = 2;
int nOld = increment(r);
        
double dd = 3.2;
int nOld = increment(dd);

Pass double by Value

OC Function

double square(double a)
{
    return a * a;
}

Calling in LabTalk Script or Command Window

[UserScript]
        
%A = 5;
type $(square(%A)); // 25
    
type square(3): $(square(3)); // square(3):9
    
int r = 2;
double rr = square(r);
type rr: $(rr); // rr: 4
    
double dd = 3.2;
double ss = square(dd);
type ss: $(ss); // ss: 10.24
    
type square(5): $(square(5)); // square(5): 25

dataset ds = data(1, 10, 1);
col(1)=ds; // put 1,2..10 to Col(1)
Col(2) = square(Col(1)); // put 1,4,...100 to Col(2);
type put square of Col(1) to Col(2), done!;

Pass double by Reference

OC Function

double increment(double& a, double dStep = 1)
{
        double dOld = a;
        a += dStep;
        return dOld;
}

Calling in LabTalk Script or Command Window

[UserScript]
                
int a = 3;
increment(a);
type -a "a = $(a)";
        
double d = 4;
increment(d, 6);
type -a "d = $(d)";

Pass string by Value

OC Function

void display(string str)
{
        printf("Your input string is %s\n", str);
}

Calling in LabTalk Script or Command Window

[UserScript]
        
display(1208);
        
display(3.1415);
        
display(good day); //literal without quotes
        
display("good bye"); //literal with quotes
        
string str$ = "Origin C";
display(str$);
        
%A = X-Functions;
display(%A);

Pass string by Reference

OC Function

string to_uppercase(string& str)
{
        string strOld = str;
        str.MakeUpper();
        return strOld; //return old value
}

Calling in LabTalk Script or Command Window

[UserScript]   
string str$ = OriginLab;
string strOld$ = to_uppercase(str$)$;
type "%(strOld$) is changed to %(str$)";

Pass int Array by Value

OC Function

int get_min_max(vector<int> vn, int& min, int& max)
{
        if ( vn.GetSize() == 0 )
                return 0;
        
        vn.Sort();
        min = vn[0];
        max = vn[vn.GetSize() - 1];
        return 1;
}

Calling in LabTalk Script or Command Window

[UserScript]
        
//loose dataset
dataset ds = data(2, 30, 2);
int min, max;
get_min_max(ds, min, max);
type "min = $(min), max = $(max)";
        
//normal dataset from column, make sure to fill some data in Col(A) to see the result.
get_min_max(Col(A), min, max);
type "min = $(min), max = $(max)";

Pass double Array by Value

OC Function

int get_min_max_double_arr(vector<double> vd, double& min, double& max)
{
        if ( vd.GetSize() == 0 )
                return 0;
        vd.GetMinMax(min, max);
        return 1;
}

Calling in LabTalk Script or Command Window

[UserScript]
        
//loose dataset
dataset ds = data(2, 30, 2);
double dMin, dMax;
get_min_max_double_arr(ds, dMin, dMax);
type "min = $(dMin), max = $(dMax)";
        
//normal dataset from column, make sure to fill some data in Col(A) to see the result.
get_min_max_double_arr(Col(A), dMin,  dMax);
type "min = $(dMin), max = $(dMax)";

Pass string Array by Value

OC Function

string string_array_set_tokens(StringArray sa, char nDelimiter = ',')
{
        string str;
        str.SetTokens(sa, nDelimiter);
        return str;
}

Calling in LabTalk Script or Command Window

[UserScript]
string mystr;
mystr$ = string_array_set_tokens(Col(A))$;
mystr$ =;

Pass Matrix as vector

OC Function

vector<double> DotMultiply(const vector<double> &vs1, const vector<double> &vs2, int nRow, int nCol) {
        matrix mat1, mat2;
        mat1.SetSize(nRow, nCol);
        mat2.SetSize(nRow, nCol);
        mat1.SetByVector(vs1, TRUE);
        mat2.SetByVector(vs2, TRUE);
        mat1.DotMultiply(mat2);
        vector<double> vs;
        mat1.GetAsVector(vs);
        return vs;
}

Calling in LabTalk Script or Command Window

[UserScript]
dataset ds;
ds = DotMultiply(Mat(1), Mat(2), wks.nrows, wks.ncols);
mean(ds)=;

Pass Matrix by range string as MatrixObject

OC Function

void test_m(string ra, string rb)
{
        MatrixObject ma(ra);
        MatrixObject mb(rb);
        int ncols = ma.GetNumCols();
        int nrows = ma.GetNumRows();
        mb.SetSize(nrows, ncols);
        
        matrixbase& m1 = ma.GetDataObject();
        matrixbase& m2 = mb.GetDataObject();
        
        m2 = m1;
}

Calling in LabTalk Script or Command Window

[UserScript]
test_m("[Mbook1]1!1","[Mbook2]1!1");

Pass Matrix by range string as Matrix

OC Function

void test_a(string ra, string rb)
{
        Matrix ma(ra);
        Matrix mb(rb);
        int ncols = ma.GetNumCols();
        int nrows = ma.GetNumRows();
        mb.SetSize(nrows, ncols);
        
        mb = ma;
}

Calling in LabTalk Script or Command Window

[UserScript]
test_a("[Mbook1]1!1","[Mbook2]1!1");

Pass object by range string in general

OC Function

#include <xfutils.h>
void test(string strRangeString)
{
        // for example passing a matrix
        MatrixObject mo;
        okxf_resolve_string_get_origin_object(strRangeString, &mo);

        /*
        Whatever you want to do next
        */
}