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
 Origin Forum
 sorting paired columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

elirosen

USA
Posts

Posted - 04/12/2006 :  3:54:47 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5 SR5
Operating System: Win XP

I have written a LabTalk script to create a summary worksheet concatenating X and Y data columns from several imported files. On import, each new worksheet is named after the file (ex: apr10001). However, concatenation doesn't occur in order of file name (i.e. apr10001, apr 10002, etc.) I'd like to sort the columns to have the paired X,Y columns ordered by column name. Can someone advise how to do this?

Currently I have named only the Y columns:

win -n d Summary_Data;

%W=%H;
doc -ef W {
if("%W"!="%H") {
%W!wks.addCol();
%W!wks.col$(%W!wks.ncols).type = 4;
%(%W,%W!wks.ncols) = %(%H,3);
%W!wks.addCol();
%W!wks.col$(%W!wks.ncols).type = 1;
%(%W,%W!wks.ncols) = %(%H,4);
%W!wks.col$(%W!wks.ncols).name$ = %H;

};
};

My attempts to use sort() have been in vain and I'm not sure I fully understand primary vs secondary sorting critieria.

Many thanks!

-Eli



Mike Buess

USA
3037 Posts

Posted - 04/12/2006 :  5:15:17 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Eli,

The following Origin C function creates a sorting worksheet and fills the first column with all X-column names from the source worksheet and the second column with the corresponding Y-column names. It then sorts that wks with respect to the second column producing the order you want. Finally it creates another worksheet and copies columns from the source wks in the order determined by the sort wks. (Probably clearer by looking at the function than reading my explanation.)
void test()
{
Worksheet wksSrc = Project.ActiveLayer();
if( !wksSrc )
return;
string wksName = wksSrc.GetPage().GetName();
Worksheet wksSort;
wksSort.Create();
for(int i=0;i<wksSrc.GetNumCols(); i+=2)
{
wksSort.SetCell(i/2,0,wksSrc.Columns(i).GetName()); // X col names to 1st col
wksSort.SetCell(i/2,1,wksSrc.Columns(i+1).GetName()); // Y col names to 2nd col
}
wksSort.Sort(1); // sort ascending w.r.t second column (Y col names)
Dataset dd1,dd2;
string str;
Worksheet wks;
wks.Create();
wks.DeleteCol(0);
wks.DeleteCol(0); // start with a blank wks
for(i=0;i<wksSrc.GetNumCols()/2;i++)
{
wksSort.GetCell(i,0,str);
wks.AddCol(str);
dd1.Attach(wksSrc.Columns(str));
dd2.Attach(wks.Columns(str));
dd2 = dd1; // copy X column
wksSort.GetCell(i,1,str);
wks.AddCol(str);
dd1.Attach(wksSrc.Columns(str));
dd2.Attach(wks.Columns(str));
dd2 = dd1; // copy Y column
}
wksSort.Destroy(); // close sort wks
wksSrc.Destroy(); // close source wks
wks.GetPage().Rename(wksName); // rename sorted wks
}


Mike Buess
Origin WebRing Member
Go to Top of Page

elirosen

USA
Posts

Posted - 04/16/2006 :  6:14:10 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Many thanks for your help, Mike. Can you tell me how modify the C code to set the copied X columns as X data. As implemented, the code leaves all columns generated in the summary worksheet as Y data.

Again, thanks for the help! This forum is invaluable.

-Eli

Edited by - elirosen on 04/16/2006 8:58:50 PM
Go to Top of Page

Deanna

China
Posts

Posted - 04/16/2006 :  11:43:46 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi, Eli.

To set a column as X data, you may just use SetType(). For example, wks.Columns(str).SetType(OKDATAOBJ_DESIGNATION_X).

Mike's code can be modified as follows:

void test()
{
Worksheet wksSrc = Project.ActiveLayer();
if( !wksSrc )
return;
string wksName = wksSrc.GetPage().GetName();
Worksheet wksSort;
wksSort.Create();
for(int i=0;i<wksSrc.GetNumCols(); i+=2)
{
wksSort.SetCell(i/2,0,wksSrc.Columns(i).GetName()); // X col names to 1st col
wksSort.SetCell(i/2,1,wksSrc.Columns(i+1).GetName()); // Y col names to 2nd col
}
wksSort.Sort(1); // sort ascending w.r.t second column (Y col names)
Dataset dd1,dd2;
string str;
Worksheet wks;
wks.Create();
wks.DeleteCol(0);
wks.DeleteCol(0); // start with a blank wks
for(i=0;i<wksSrc.GetNumCols()/2;i++)
{
wksSort.GetCell(i,0,str);
wks.AddCol(str);

wks.Columns(str).SetType(OKDATAOBJ_DESIGNATION_X);//Set the column to X

dd1.Attach(wksSrc.Columns(str));
dd2.Attach(wks.Columns(str));
dd2 = dd1; // copy X column
wksSort.GetCell(i,1,str);
wks.AddCol(str);
dd1.Attach(wksSrc.Columns(str));
dd2.Attach(wks.Columns(str));
dd2 = dd1; // copy Y column
}
wksSort.Destroy(); // close sort wks
wksSrc.Destroy(); // close source wks
wks.GetPage().Rename(wksName); // rename sorted wks
}


Deanna
OriginLab GZ Office

Edited by - Deanna on 04/17/2006 9:45:36 PM
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