Author |
Topic  |
|
a.abc.b35
175 Posts |
Posted - 02/02/2011 : 7:53:38 PM
|
I have a origin C code where i create a blank worksheet with user input names for the columns (both short and wrong). Plus I set them as X,Y,Y_err. I have 3 X columns, and 3 sets of Y and Y_err columns in the worksheet. Now I want that the last set of Y and Y_err column is calculated from 1st two sets of Y and Y_err such as: Y3rd set = Y2nd set - Y1st set; Yerr3rd set = (sqrt(Yerr2nd set)^2 - (Yerr1st set)^2); So that the green button (which means automatic calculation is activated) on the 3rd sets of Y column is visible with the above formulas in them. Can anyone help me with that please ? I hope the question is clear.
AB |
|
a.abc.b35
175 Posts |
Posted - 02/02/2011 : 10:53:50 PM
|
Following is the code (relevant part) I wrote to solve my problem. It is solved but I want to know if there is a way to improve the code and make it smaller. PLease help. .................................... code is below ..................................... // function to create worksheet for option 2 in switch statement static void MolTiltWks(string &wpName) { if (!wpName.IsEmpty()) { // Create workbook WorksheetPage wp; wp.Create("origin", CREATE_VISIBLE); if( !wp ) { printf("From MolTiltWks: Invalid workbook.Operation Cancelled!"); return; } wp.SetName(wpName); // sets the name of the workbook as given by user // sets the total number of columns of worksheet1 of the workbook Worksheet wks= wp.Layers(0); wks.SetSize(-1,9); // nRows = -1 (keeps current no of rows),nCol=9(total no. of columns) if(wks.IsValid()) { // set column width foreach(Column cc in wks.Columns) { cc.SetWidth(8); // set column width [BOOL SetWidth( int iWidth = -1(default) )] }
// Set short name,long name, label, size ,type etc. string NameC0 = "File"; string NameC1 = "Strain"; string NameC2 = "Time"; string NameC3 = "XcLA"; string NameC4 = "XcLA_Err"; string NameC5 = "XcSA"; string NameC6 = "XcSA_Err"; string NameC7 = "MolTilt"; string NameC8 = "MolTilt_Err"; string unitC1 = "%"; string unitC2 = "min"; string unitC3 = "degree"; string unitC4 = "degree"; string unitC5 = "degree"; string unitC6 = "degree"; string unitC7 = "degree"; string unitC8 = "degree"; wks.Columns(0).SetName(NameC0); // change the name of the 1st column wks.Columns(1).SetName(NameC1); // change the name of the 2nd column wks.Columns(2).SetName(NameC2); // change the name of the 3rd column wks.Columns(3).SetName(NameC3); // change the name of the 4th column wks.Columns(4).SetName(NameC4); // change the name of the 5th column wks.Columns(5).SetName(NameC5); // change the name of the 6th column wks.Columns(6).SetName(NameC6); // change the name of the 7th column wks.Columns(7).SetName(NameC7); // change the name of the 8th column wks.Columns(8).SetName(NameC8); // change the name of the 9th column wks.Columns(0).SetLongName(NameC0); // change the name of the 1st column wks.Columns(1).SetLongName(NameC1); // change the name of the 2nd column wks.Columns(2).SetLongName(NameC2); // change the name of the 3rd column wks.Columns(3).SetLongName(NameC3); // change the name of the 4th column wks.Columns(4).SetLongName(NameC4); // change the name of the 5th column wks.Columns(5).SetLongName(NameC5); // change the name of the 6th column wks.Columns(6).SetLongName(NameC6); // change the name of the 7th column wks.Columns(7).SetLongName(NameC7); // change the name of the 8th column wks.Columns(8).SetLongName(NameC8); // change the name of the 9th column
wks.Columns(0).SetType(OKDATAOBJ_DESIGNATION_X); // sets it as 'X' wks.Columns(1).SetType(OKDATAOBJ_DESIGNATION_X); // sets it as 'X' wks.Columns(2).SetType(OKDATAOBJ_DESIGNATION_X); // sets it as 'X' wks.Columns(3).SetType(OKDATAOBJ_DESIGNATION_Y); // sets it as 'Y' wks.Columns(4).SetType(OKDATAOBJ_DESIGNATION_ERROR); // sets it as 'ERR_Y' wks.Columns(5).SetType(OKDATAOBJ_DESIGNATION_Y); // sets it as 'Y' wks.Columns(6).SetType(OKDATAOBJ_DESIGNATION_ERROR); // sets it as 'ERR_Y' wks.Columns(7).SetType(OKDATAOBJ_DESIGNATION_Y); // sets it as 'Y' wks.Columns(8).SetType(OKDATAOBJ_DESIGNATION_ERROR); // sets it as 'ERR_Y' wks.Columns(1).SetUnits(unitC1); wks.Columns(2).SetUnits(unitC2); wks.Columns(3).SetUnits(unitC3); wks.Columns(4).SetUnits(unitC4); wks.Columns(5).SetUnits(unitC5); wks.Columns(6).SetUnits(unitC6); wks.Columns(7).SetUnits(unitC7); wks.Columns(8).SetUnits(unitC8); wks.AutoSize();// autosizes the worksheets after column renames string comment = "Molecular Tilt Calculated from LA and SA peak position"; for(int k1=0; k1<wks.GetNumCols();k1++) wks.Columns(k1).SetComments(comment); // Set Formula for columns // 1st,get column names and stores in a string vector vector<string> ColName; string strName; for(int k=0; k<wks.GetNumCols();k++) { wks.Columns(k).GetName(strName); ColName.Add(strName); //printf("Column %u is named %s\n", k+1, ColName[k]); } // 2nd,Stores the formulas for the columns in two strings. Be very careful here in adding the strings string formula7,formula8; formula7 = "Col("+ColName[3]+")"+"-"+"Col("+ColName[5]+")"; formula8 = "sqrt("+"Col("+ColName[4]+")^2 + Col("+ ColName[6]+")^2)"; // 3rd,Sets the Column formula here for the respective columns wks.Columns(7).SetFormula(formula7,AU_AUTO); //wks.Columns(7).ExecuteFormula(); wks.Columns(8).SetFormula(formula8,AU_AUTO); //wks.Columns(8).ExecuteFormula(); return; } else { printf("Workbook name to store the data is empty.Operation aborted!"); } } }
AB |
Edited by - a.abc.b35 on 02/02/2011 11:32:41 PM |
 |
|
Iris_Bai
China
Posts |
Posted - 02/09/2011 : 03:44:18 AM
|
Hi,
For column Name, Long Name, Units, can use vector<string>, for example,
vector<string> vsNames = {"File", "Strain", "Time", "XcLA", "XcLA_Err", "XcSA", "XcSA_Err", "MolTilt", "MolTilt_Err"};
//vector<string> vsLNames = {...};
//vector<string> vsUnits = {...};
int index=0;
foreach(Column col in wks.Columns)
{
col.SetNames(vsNames[index]);
col.SetLongName(vsLNames[index]);
col.SetUnits(vsUnits[index]);
}
For column type, can use Worksheet::SetColDesignations, for example,
wks.SetColDesignations("XYLYL")
BTW, pls put codes in in code format, click # button (the thrid from end) to insert the format notation to forum edit window, then long code will display as nice.
Iris |
Edited by - Iris_Bai on 02/09/2011 03:54:50 AM |
 |
|
|
Topic  |
|
|
|