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
 Script before Formula in worksheet
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

VanAwful

USA
13 Posts

Posted - 08/24/2019 :  9:32:39 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
I'm having an issue with a very simple process. In my worksheet I want to use If-Else to compare Col(H) to 0. If true then set x=0, if false set x=1. This is the code:


if (Col(H)==0)
{
  x=0;
}
Else
{
  x=1;
}


I then put x in the formula window. When I apply, it evaluates all rows as 0.

Not sure what I am doing wrong. Yes, I know I can use an IF-ELSE in the formula field. I am doing it this way as a start to a much larger script, but I need to understand how to make this IF-ELSE work in the script first before proceeding.

UPDATE: Appears it is only evaluating on the first row then applying to all rows. How do I make the LabTalk script process against each row?


Edited by - VanAwful on 08/25/2019 09:15:49 AM

yuki_wu

896 Posts

Posted - 08/26/2019 :  02:41:21 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Just use if function like:
if(col(A)==0, 0, 1)
https://www.originlab.com/doc/LabTalk/ref/If-func

Regards,
Yuki
OriginLab
Go to Top of Page

aplotnikov

Germany
169 Posts

Posted - 08/26/2019 :  04:55:01 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi!

Just a remark.

If()-function was implemented since the ver. 2019SR0. If you have an earlier version you should use the ternary (conditional) operator "?:" which works in the recent versions as well.
Go to Top of Page

VanAwful

USA
13 Posts

Posted - 08/26/2019 :  08:48:30 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I am using version 2019b.

Using if(col(A)==0, 0, 1) will not work for what I need. The example I provided is only the start. I need to make it work first.


if (Col(H)==0)
{
  x=0;
  <write row values to different sheet in next open row of col(A)>
}
Else
{
  x=1;
  <write rest of script based on x=1>    
}

<provide new value for x to use in cell>
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 08/26/2019 :  1:42:22 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
if more complicated code needed, then you may consider using LabTalk function, like this



Before formula script is executed only once, so you define a function there and use it as column formula.

You can google "Labtalk Function" for more details.

CP
Go to Top of Page

VanAwful

USA
13 Posts

Posted - 08/26/2019 :  3:35:12 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
@cpyang

Thank You! That is what I was looking for.
Go to Top of Page

aplotnikov

Germany
169 Posts

Posted - 08/27/2019 :  03:20:31 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I'm just wondering why not to use the simpliest formula for Col(J) values? It works for sure in any Origin version.

Col(H)==0 ? 0 : 1


What is the reason to define an additional function?
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 08/28/2019 :  1:12:41 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by aplotnikov

I'm just wondering why not to use the simpliest formula for Col(J) values? It works for sure in any Origin version.

Col(H)==0 ? 0 : 1


What is the reason to define an additional function?



His example code is to just illustrate his point, but his actual code is much more complicated and cannot be expressed as an expression.

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