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
 Forum for Origin C
 Calculation between 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

Thomas83

24 Posts

Posted - 08/05/2013 :  10:46:06 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin 8 SR6
Win7

Hi.
I have one workbook (WKB1) with 5 Worksheets(wks1...wks5). I want to sum up all rows of each column and this for each column and worksheet. Furthermore, this "sum" of each column should also be summed up. In the end I want to have one number which is the sum off all values of a worksheet. In LabTalk this is easy but I think it can be much faster in Origin C.
Does anyone have an idea?
Thanks a lot.

Penn

China
644 Posts

Posted - 08/05/2013 :  11:13:57 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Basically, what you want may include the following aspects.
1. Loop all worksheets in workbook, see Accessing Worksheets in a Workbook, the foreach statement can help.
2. Sum up all rows, please refer to ocmath_row_desc_stats function.
3. Sum up values in a vector, see Sum method.

Penn
Go to Top of Page

Thomas83

24 Posts

Posted - 08/06/2013 :  06:26:25 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi.
I think I need further help. Could you give an explizite example.

#include <Origin.h>

void SumUp_Worksheets()
{


	WorksheetPage  wksPage("[Test]");
		foreach (Layer wks in wksPage.Layers)
		{
			DataRange dr;
			//string strRangeName ="X";
			int r1=0, c1=0, r2=10, c2=1;
			dr.Add("Range1",wksPage, r1, c1, r2, c2);
			matrix myData;
			dr.GetData(myData);
			
			int nRows = wks.GetNumRows();
			vector vSum(nRows);
			int nRet = ocmath_row_desc_stats(nRows, wks, vSum);
		}

}
Go to Top of Page

Castiel

343 Posts

Posted - 08/06/2013 :  09:50:13 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
#include <Origin.h>
void SumUp_Worksheets()
{
	WorksheetPage wksPage("Test");
	if( !wksPage )
		return;
	
	int nwks = wksPage.Layers.Count();
	vector<double> sums( nwks );
	double sum;
	
	Worksheet wks;
	int i;
	for( i = 0; i < nwks; i++ )
	{
		wks = wksPage.Layers( i );
		
		foreach( Column col in wks.Columns )
		{
			vectorbase & vb = col.GetDataObject();
			vb.Sum( sum );
			sums[i] += sum;
		}
		
		printf("Sum of [%s]%s: %g\n", wksPage.GetName(), wks.GetName(), sums[i] );
	}
	
	return;
}


妾+   午旦  妹罕妾  妾伊    用仇  妾/     岫ㄞ
 妾京用 仍巨  件 侈   件戶' 甘岫平   /欠  白岫妹
   併             艮          岫  奈 白   岫
                              岫
Go to Top of Page

Penn

China
644 Posts

Posted - 08/06/2013 :  11:46:34 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Please refer to the example below:

void SumUp_Worksheets()
{
	WorksheetPage wksPage("Book1");  // Workbook
	if(!wksPage)
		return;
	foreach(Layer layer in wksPage.Layers)  // All worksheets in workbook
	{
		Worksheet wks(layer);
		DataRange dr;  // Data range to the whole worksheet
		dr.Add(wks, 0, "X", -1);
		matrix mData;
		dr.GetData(mData);  // Get worksheet data into matrix
		int nRows = mData.GetNumRows();  // number of rows
		int nCols = mData.GetNumCols();  // number of columns
		vector vSum(nRows);  // vector to strore sum of rows in each worksheet
		// Sum up each rows in all columns
		int nRet = ocmath_row_desc_stats(nRows, nCols, mData, NULL, NULL, NULL, NULL, NULL, vSum);
		int nn = wks.AddCol();  // Add a column to store these sums
		Column col(wks, nn);
		col.SetLongName("Sum of Rows");
		vectorbase& vv = col.GetDataObject();
		vv = vSum;
		
		// Sum up each columns in worksheet
		for(int ii = 0; ii < nCols; ii++)
		{
			Column colSrc(wks, ii);
			vectorbase& vSrc = colSrc.GetDataObject();
			double dSum;
			vSrc.Sum(dSum);
			nn = wks.AddCol();  // Add a column to hold the sum in the first cell
			wks.Columns(nn).SetLongName("Sum of Column "+colSrc.GetName());
			wks.SetCell(0, nn, dSum);			
		}
		
		// Sum up the whole worksheet
		double dWksSum;
		nn = wks.AddCol();  // Add a column to hold the sum of worksheet in the first cell
		wks.Columns(nn).SetLongName("Sum of All Values in Sheet");
		vv.Sum(dWksSum);
		wks.SetCell(0, nn, dWksSum);
	}
}


Penn
Go to Top of Page

Thomas83

24 Posts

Posted - 08/07/2013 :  02:28:24 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi castiel, hi Penn.
Thanks a lot. Both codes work.
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