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
 Finding max value and writing to a new column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

bur2000

Germany
36 Posts

Posted - 03/10/2014 :  07:22:20 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 9.0

I want to find the maximum value of a worksheet and write it to a new column. Here's my script:

// Filling the two columns from empty worksheet with some data
range activeCol = 1;
activeCol = {1:14};
range activeCol = 2;
activeCol = {15:28};

//maxValue is set to max of first column
double maxValue;
range activeCol = 1;
maxValue = max(activeCol);

// Cycle through the remaining columns and update maxValue if necessary
for ( ii = 2; ii <= wks.ncols; ii++)
{
    range activeCol = ii;
    if (maxValue < max(activeCol)) maxValue = max(activeCol);
}

// Create new column and write maxValue to it
range normalizedCol = Col("Normalized");
normalizedCol[1] = maxValue;

// Output the variable for testing purposes
type -b $(maxValue);


I have two problems/questions.

First of all, to fill the columns with data, do I really need to declare activeCol each time? I wasn't able to switch activeCol to the second column by doing activeCol = 2.

More importantly, after running the script, I end up with 14 being written to Col(Normalized). It seems like range activeCol = ii; doesn't work as supposed. Because if I substitute it with range activeCol = 2; everything works fine.

Edited by - bur2000 on 03/10/2014 08:01:05 AM

lkb0221

China
497 Posts

Posted - 03/10/2014 :  09:18:14 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi#65292;

1). Assignment range variable directly will make it ambiguous.
For example:
//
range rr = col(1)[2]; // Point to the second cell of col(A)
rr = 2;
//
The second line will put value of 2 into this cell, instead of change rr to col(B). So we should declare it's a range notation changing, when we mean it.

2). You would need to use $(ii) instead of just ii, in the range declaration in the loop. Sometimes LabTalk would need those substitution notations to help run-time value passing.

Zheng
OriginLab
Go to Top of Page

bur2000

Germany
36 Posts

Posted - 03/11/2014 :  04:53:48 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
The second line will put value of 2 into this cell, instead of change rr to col(B). So we should declare it's a range notation changing, when we mean it.
How do I change the range a variable is referencing? For example
range rr = 2;
rr[1] = 1;
//Now I want to set rr to point to Col(2)
rr = 2
//as you said, it's not working
So I would need to use range rr = 2? This feels a little strange to me. In other languages you can't do int a = 3; print(a); int a = 4;. This will give an error. But in Labtalk that's how you do it?

quote:
2). You would need to use $(ii) instead of just ii, in the range declaration in the loop. Sometimes LabTalk would need those substitution notations to help run-time value passing.
Thanks, is there any rule for when to use $(ii)?

Edited by - bur2000 on 03/11/2014 04:55:27 AM
Go to Top of Page

lkb0221

China
497 Posts

Posted - 03/11/2014 :  09:20:13 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Yes, currently you need to specify "range" when changing.
I admit that a little confusing, I'll talk to our developers to see if there is something we can improve.

Zheng
OriginLab
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