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
 Pointer on vector; mask
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

MAN-Techno

Germany
Posts

Posted - 02/05/2007 :  03:51:55 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version 7.5G SR6
Operating System: Windows XP SP2

Hi everyone,
I am a new origin user and tried my first steps with origin C.
I wanted to perform a simple analysis with 2 y-columns of a worksheet and store the result in a third column.

If the value using for the division is too small I want to mask the data.

My code (simplified) is the following:

// Aufruf: Data3_B=Bsp4(Data1_B,Data2_B,3.0)
vector<double> Bsp4(vector<double>& vec1,vector<double>& vec2) {
vector<double> *vecOut;
int iRows,i;

iRows=vec1.GetSize();
vecOut= new (vector<double>);
// vecOut= (vector<double> *) malloc(sizeof(vector<double>));
(*vecOut).SetSize(iRows);

for (i=0; i<iRows;i++) {
(*vecOut)[i] = vec1[i] / vec2[i];
if (vec2[i] < 1.0)
// mask data
}

return *vecOut;
}

I can not allocate memory either with the new nor with the malloc function.
(Fehler, allgemeiner Kompilierungsfehler; I try to translate: Error, general compiling error)

Second problem: I could not find something in the manuals how I can
perform the masking operation with Origin C.

Please excuse my terrible english
and thanks in advance for your help!

Thomas

easwar

USA
1965 Posts

Posted - 02/05/2007 :  10:12:02 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Thomas,

First of all, you can simply work with existing Origin objects such as dataset columns to do your operation. You don't have to allocate memory etc.

Try the following code where I am assuming your input data is in cols 2,3 of the workshet and the output column is col 4. You can make changes according to your situation and try.

Easwar
OriginLab


void divide_cols()
{
// Assume data already exists in a worksheet and worksheet is active
// Point to worksheet and check validity of worksheet object
Worksheet wks = Project.ActiveLayer();
if( !wks ) return;

//Assume your input y cols are col 2 and 3 and you want output in col 4
// Assume all columns exist, check for validity
Dataset dsY1(wks, 1);
Dataset dsY2(wks, 2);
Dataset dsY3(wks, 3);
if( ! dsY1 || !dsY2 || !dsY3 ) return;

// Assume input cols 1 and 2 are same size
// Get size of one of the input cols and set the output col to same size
int nSize = dsY1.GetSize();
dsY3.SetSize(nSize);
// Get and store name of the dataset for the output col
string strResultDatasetName = dsY3.GetName();

// Loop over all rows
for(int i = 0; i < nSize; i++)
{
// Perform the division
dsY3[i] = dsY1[i] / dsY2[i];
// Now, if second col value is less than 1.0, then...
if( dsY2[i] < 1.0 )
{
// In order to mask, need to call back to LabTalk
// Create a string such as "data1_d<4>=1" which is LT command to mask specific row
// LabTalk row index starts at 1, so need to add offset of 1 to row number
string strCMD;
strCMD.Format("%s<%d>=1;", strResultDatasetName, i + 1);
// Execute LabTalk string to mask this row
LT_execute(strCMD);
}
}
}



Go to Top of Page

MAN-Techno

Germany
Posts

Posted - 02/06/2007 :  03:39:48 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Easwar,

thank you very much for your big help.
Still one question is remaining - could you help me again?

My idea was to associate my function directly with a column.
(Spaltenwerte berechnen, automatisch -> calculate columnvalues, automatic).

The columns used for input were passed as arguments.
See my definition below:
// Call: Data3_B=Bsp4(Data1_B,Data2_B)
vector<double> Bsp4(vector<double>& vec1,vector<double>& vec2);

How must I adapt your routine
void divide_cols();
to use it as a function to which I can pass columns e.g. Data1_B, Data2_B and returns a value Data3_B?

Must I transform vectors in datasets or what is the best way?

Thanks again!
Thomas
Go to Top of Page

easwar

USA
1965 Posts

Posted - 02/06/2007 :  09:48:23 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Thomas,

You can write a function such as:



vector add_ds(string str1, string str2)
{
Dataset ds1(str1);
Dataset ds2(str2);
vector vec;
vec = ds1+ds2;
return vec;
}


and call it from script with command such as
data1_d = add_ds(data1_b, data1_c)

BUT, you wanted to also set the rows of data1_d to be masked, and masking is a proprety of the datset column, so that cannot be set in the vector which is returned.

So you could consider instead to write the function like below, by passing all three names as arguments, and then calling from script such as:
divide_cols(data1_b, data1_c, data1_d);



void divide_cols(string strDS1, string strDS2, string strDS3)
{
Dataset ds1(strDS1);
Dataset ds2(strDS2);
Dataset ds3(strDS3);
if( !ds1 || !ds2 || !ds3 ) return;

int nSize = ds1.GetSize();
ds3.SetSize(nSize);

// Loop over all rows
for(int i = 0; i < nSize; i++)
{
// Perform the division
ds3[i] = ds1[i] / ds2[i];
// Now, if second col value is less than 1.0, then...
if( ds2[i] < 1.0 )
{
// In order to mask, need to call back to LabTalk
// Create a string such as "data1_d<4>=1" which is LT command to mask specific row
// LabTalk row index starts at 1, so need to add offset of 1 to row number
string strCMD;
strCMD.Format("%s<%d>=1;", strDS3, i + 1);
// Execute LabTalk string to mask this row
LT_execute(strCMD);
}
}
}



Hope this helps.

Easwar
OriginLab

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