The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 How to call labtalk code?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

EigenGoofy

64 Posts

Posted - 06/16/2011 :  08:39:05 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 8.5


For example, I have the following code:

del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";



The Origin help files said that if wanna call this, then the code must start with (for instance): void myfunc(), so I added:

void myfunc()
del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";


then in the script window, I type "myfunc", but "Command error" appears.

Question:
So How do I call the code please?

Thank You!

SteffenG

Germany
Posts

Posted - 06/16/2011 :  09:33:46 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hello EigenGoofy,

you have to enter first the keyword 'function' and put the code of the function inside brackets like:
function myfunc(){
del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";
}

The keyword 'void' does not exist in LabTalk.

Best regards,
Steffen

Edited by - SteffenG on 06/16/2011 09:41:19 AM
Go to Top of Page

EigenGoofy

64 Posts

Posted - 06/16/2011 :  10:21:23 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hello, SteffenG

This can not work.

Save the following code as "my func.ogs":
function myfunc(){
del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";
}


then in the script window, type "myfunc".

it returns "Command error".

Thank You!




quote:
Originally posted by SteffenG

Hello EigenGoofy,

you have to enter first the keyword 'function' and put the code of the function inside brackets like:
function myfunc(){
del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";
}

The keyword 'void' does not exist in LabTalk.

Best regards,
Steffen


Edited by - EigenGoofy on 06/16/2011 10:22:02 AM
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 06/16/2011 :  1:03:47 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
In order to define functions in an OGS and use in other places, you need to add the @global=1 before the function is defined. See

http://wiki.originlab.com/~originla/wiki/index.php?title=LabTalk:Data_Types_and_Variables#Forcing_Global_Scope


CP
Go to Top of Page

EigenGoofy

64 Posts

Posted - 06/16/2011 :  5:23:29 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by cpyang

In order to define functions in an OGS and use in other places, you need to add the @global=1 before the function is defined. See

http://wiki.originlab.com/~originla/wiki/index.php?title=LabTalk:Data_Types_and_Variables#Forcing_Global_Scope


CP




It works, Thank You, cpyang!

Actually, you only give me a PARTIAL answer. There are at least 3 unsolved problems. Luckily, I solved all of them

If saving the following code as "myfunc.ogs" in the folder User Files

@global=1;
function myfunc(){
del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";
}

then type "myfunc" in the script window, it will return "command error".

Solution: after saving this code as an ogs file, we have to completely exit Origin, before calling the function in the script window, otherwise it won't work.


After rebooting Origin, then in the script window type "myfunc", it does not appear "command error", but just nothing happens. type "myfunc" again, nothing happens again.

Solution: This is a naming problem, we can not name the file as the same as the name of the calling function. Let's rename "myfunc.ogs" by "myfunc1.ogs".


Aftering restarting Origin again, type "myfunc" in the script window. it appears "command error".

Solution : we have to type "myfunc1" first, at this time, it does not return "command error", then type "myfunc", up to now, it successfully calls my function!

Thank You again, cpyang!

Edited by - EigenGoofy on 06/16/2011 5:27:13 PM
Go to Top of Page

EigenGoofy

64 Posts

Posted - 06/16/2011 :  9:31:49 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
By the way, based on my last post, the following address provides insufficient help:

http://wiki.originlab.com/~originla/wiki/index.php?title=LabTalk:Data_Types_and_Variables#Forcing_Global_Scope

Go to Top of Page

SteffenG

Germany
Posts

Posted - 06/17/2011 :  04:53:03 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
If you want to use OGS-files you have to take the run.section-method. You will find help for this in the LabTalk-Help under topic 'run'.

Best regards,
Steffen


quote:
Originally posted by EigenGoofy

Hello, SteffenG

This can not work.

Save the following code as "my func.ogs":
function myfunc(){
del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";
}


then in the script window, type "myfunc".

it returns "Command error".

Thank You!



Go to Top of Page

cpyang

USA
1406 Posts

Posted - 06/17/2011 :  08:16:02 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
We greatly appreciate EigenGoofy's sharing of info and we will work on fixing that wiki page.

One thing definitely true is that ogs file name should not be the same as LT Function name since ogs files in user folder can be executed by name (calling the [main] section if present), see

http://wiki.originlab.com/~originla/wiki/index.php?title=Script:From_Files#Running_an_OGS_File

But one key think not mentioned in that wiki is that spaces in the ogs file name is striped, so the myfunc will execute "my func.ogs".

We can certainly change the precedence order to allow LT function higher precedence to avoid this confusion in future versions.

CP
Go to Top of Page

EigenGoofy

64 Posts

Posted - 06/20/2011 :  9:50:54 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply

[Main]
del col(C);
wks.addcol(C);
col(C)[C]$="sum/N";

Save this code as "func2.ogs" in my User Files folder.

Then in the script window type "run.section(func2, Main)".

Yes, yes, this works even more convenient.

Thank you very much,SteffenG and cpyang!
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 06/21/2011 :  07:56:45 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Yes, for basically a procedure, you are better off using ogs sections.

Functions are useful when you need a return value, and when you need to pass in arguments that are not easy to achieve with %1 %2 etc.

CP
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000