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
 Maximum values in a new sheet
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Clairekun

Germany
175 Posts

Posted - 02/18/2013 :  03:41:26 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
Operating System: Windows 7

Hello,

I have a worksheet with one X column and many Y columns. I would need to find the maximum value for every Y column and paste all those X-Y coordinates in a new sheet or book. How could I do this?

There are similar posts here, but I couldn't find a solution that fitted my needs. Sorry if I missed it.

greg

USA
1378 Posts

Posted - 02/18/2013 :  4:32:50 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
This should do it:

range raw = [Book1]Sheet1; // Your book and sheet
ncols = raw.ncols; // total number of your columns (assume x is first)
newbook;
range rax = 1; // column for x output
range ray = 2; // column for y output
range radx = raw!1; // The original x column
// loop over all y columns
for( ii = 2 ; ii <= ncols ; ii++ )
{
range rad = raw!$(ii); // next y column
limit rad; // Puts some stats into tree LIMIT.
rax[ii-1] = radx[limit.imax]; // Get x of row with maximum
ray[ii-1] = rad[limit.imax]; // Get y of row with maximum
}
Go to Top of Page

Clairekun

Germany
175 Posts

Posted - 02/26/2013 :  04:42:17 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you, it works like a charm!

If I needed to loop through several worksheets, though, how could I do it? I haven't found this option under "Range Notation" in the wiki, and setting no range at all (so that it would be applied to all workbooks) results in the creation of endless new workbooks until Origin crashes.
Go to Top of Page

cdrozdowski111

USA
247 Posts

Posted - 02/27/2013 :  7:33:44 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The following will go through every worksheet of every workbook in the current project (it might not be the most efficient code but seems to work okay based on a quick test):


dataset dsXCol = {}; // Dataset to hold X column data for new workbook- assigned at end of script
dataset dsYCol = {}; // Dataset to hold Y column data for new workbook

string strName$; // String to hold name of new book
newbook result:= strName$; // Create new workbook putting it's name into strName$

int jj = 1; // Counter for adding rows to above datasets


// Loop over every worksheet in project
doc -e LB {

	if (exist(%H, 2) == 0) // Not a workbook, must be a matrix
	{
		continue;
	}

	if (%H == strName$) // Skip newly created workbook
	{
		continue;
	}

	range rngX = col(1); // The current worksheet X column

	// Loop over all Y columns
	for(int ii = 2 ; ii <= wks.ncols; ii++)
	{
		range rngY = col($(ii)); // The current worksheet Y column

		limit rngY; // Puts some stats into tree LIMIT

		dsXCol[jj] = rngX[limit.imax]; // Get X of row with maximum. Put into dsXCol dataset using jj counter
		dsYCol[jj] = rngY[limit.imax]; // Get Y of row with maximum. Put into dsYCol dataset
		
		jj++; // Increment jj counter for adding rows to above datasets
	}
}


range rngCol1 = [%(strName$)]Sheet1!col(1); // Get column 1 of newly created workbook, sheet 1
range rngCol2 = [%(strName$)]Sheet1!col(2); // Get column 2 of newly created workbook, sheet 1

rngCol1 = dsXCol; // Assign X dataset to column 1
rngCol2 = dsYCol; // Assign Y dataset to column 2

Go to Top of Page

cdrozdowski111

USA
247 Posts

Posted - 03/01/2013 :  08:05:29 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The code below is a bit of an improvement. It allows you to either go through every worksheet in the whole project or only those in a selected folder in Project Explorer (except that it will NOT go into subfolders if this method is chosen).

If you look in the code there will be two "doc" commands, one of which is commented out. You can change which one is commented out to change the behavior according to your wishes.


string strName$; // String to hold name of new book
newbook result:= strName$; // Create new workbook putting it's name into strName$

range rngNewCol1 = [%(strName$)]Sheet1!col(1); // Get column 1 of newly created workbook, sheet 1
range rngNewCol2 = [%(strName$)]Sheet1!col(2); // Get column 2 of newly created workbook, sheet 1

int jj = 1; // Counter for adding rows to columns in new workbook


// !!! The following command loops over every worksheet in the whole project
doc -e LB {

// !!! The following command loops over every worksheet in a selected folder in Project Explorer. Does NOT look in subfolders
//doc -ef LB {


	if (exist(%H, 2) == 0) // Not a workbook, must be a matrix
	{
		continue;
	}

	if (%H == strName$) // Skip newly created workbook
	{
		continue;
	}

	range rngX = col(1); // The current worksheet X column

	// Loop over all Y columns in current worksheet
	for(int ii = 2 ; ii <= wks.ncols; ii++)
	{
		range rngY = col($(ii)); // The current worksheet Y column

		limit rngY; // Puts some stats into tree LIMIT

		rngNewCol1[jj] = rngX[limit.imax]; // Get X of row with maximum. Put into column 1 of newly created workbook
		rngNewCol2[jj] = rngY[limit.imax]; // Get Y of row with maximum. Put into column 2 of newly created workbook
		
		jj++; // Increment jj counter for adding rows to columns
	}
}
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