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
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Calculation between columns

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
Thomas83 Posted - 08/05/2013 : 10:46:06 AM
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.
5   L A T E S T    R E P L I E S    (Newest First)
Thomas83 Posted - 08/07/2013 : 02:28:24 AM
Hi castiel, hi Penn.
Thanks a lot. Both codes work.
Penn Posted - 08/06/2013 : 11:46:34 PM
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
Castiel Posted - 08/06/2013 : 09:50:13 AM
#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;
}


©c¡Ï   ¤È¥¹  ©f¨u©c  ©c¥ì    ¥Î¤³  ©c/     ©¦£¯
 ©c¨Ê¥Î ¤´¥¨  ¥ó ¨×   ¥ó¤á' ¥Ì©¦¥­   /¤í  ¥Õ©¦©f
   ¨Ö             ¦á          ©¦£þ  ©` ¥Õ   ©¦
                              ©¦
Thomas83 Posted - 08/06/2013 : 06:26:25 AM
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);
		}

}
Penn Posted - 08/05/2013 : 11:13:57 PM
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

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000