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
 Importing 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

lauransl

6 Posts

Posted - 01/31/2013 :  04:27:34 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 8
Operating System: Windows XP

Hi, I have a small program which opens different data files with a defined format (which I managed with the help of Greg, thanks!).
Now, I want to extract the last column H (or col(7)) of all the opened files into a different wks called Energy in the form:
Col(0) Col(1) Col(2) Col (3) ...
Energy Col H1 Col H2 Col H3 ...

I tried to extract the column H each time I opened a file, that way if I run the program twice a day I can continue introducing the col H# into my Energy wks.

But... it doesn't work. Here is my code, if someone could help me with this, it would be really great!

Thanks a lot!

Laura

The code:


#include <Origin.h>
#include <XFbase.h>

void importfinal1()
{
// prepare worksheet with data to do the extract
Worksheet wksfin;
wksfin.Create("Energy-dIdV");
wksfin.SetSize(2000, 1); // set worksheet with # rows and # columns

Column colA;
colA.Attach(wksfin, 0);
colA.SetName("Energy");

// The first part is to open multiple files

string strXFdlg = "dlgfile";
XFBase xf(strXFdlg);
if( !xf.IsValid() )
printf("Failed to attach to XFunction\n");
else
{
// Setup
string strNames, strGroup, strTitle, strInit;
int iMulti;
// Or use FDLOG Group names - like ASCII, Image
strGroup = "*.*";
strTitle = "My Import Dialog"; // Your own title
strInit = "D:\\Temp\\"; // Path to open in
iMulti = 1; // Use 0 for just one file

xf.SetArg("fname", strNames);
xf.SetArg("group", strGroup);
xf.SetArg("title", strTitle);
//xf.SetArg("init", strInit);
xf.SetArg("multi", iMulti);
xf.Run(0);
for( int idx = 0 ; idx < strNames.GetNumTokens('\x0D') ; idx++ )
{
string strFile = strNames.GetToken(idx,'\x0D');
strFile.TrimLeft();

ASCIMP ai;

if(AscImpReadFileStruct(strFile, &ai)==0)
{
ai.iPartial = 1; // 1 to specify partial import
ai.iPartialC1 = 0; // import from 2nd column (0 offset)
ai.iPartialC2 = 7; // set the last column, so here just import 4 columns
ai.iPartialR1 = 130; // set the import row from
ai.iPartialR2 = 2000; // set the import row to

ai.iMaxLabels = 0; // Not set any header line to Comments label

ai.nNumSep = NF_IS_AMERICAN;

ai.iMode = 0; // import as New Wks(0), New Columns (1), New Rows (2)

Worksheet wks;
wks.Create();

if(0 == wks.ImportASCII(strFile, ai))
{
// Set user parameter labels to specified names
Grid grid;
grid.Attach(wks);
vector<string> vsUserLabels = {"Expanded Discription", "Type Indication"};
grid.SetUserDefinedLabelNames(vsUserLabels);

Column colA;
colA.Attach(wks, 0);
colA.SetName("Energy");

Column colB;
colB.Attach(wks, 1);
colB.SetName("Current Fwd");

Column colC;
colC.Attach(wks, 2);
colC.SetName("Input 3 Fwd");

Column colD;
colD.Attach(wks, 3);
colD.SetName("Input 4 Fwd");

Column colE;
colE.Attach(wks, 4);
colE.SetName("Current Bwd");

Column colF;
colF.Attach(wks, 5);
colF.SetName("Input 3 Bwd");

Column colG;
colG.Attach(wks, 6);
colG.SetName("Input 4 Bwd");

Column colH;
colH.Attach(wks, 7);
colH.SetName("dI/dV Mean");

Column col(wks, 7);
col.SetFormula("(col(2)+col(5))/2", AU_AUTO, 0, 2000); // set column formula with auto update mode
col.ExecuteFormula();// this step is needed to initiate Recaulation

int nBegin, nEnd;
bool bAU = col.IsFormulaAutoUpdate(&nBegin, &nEnd);
if(AU_AUTO == bAU)
out_str("set last column formula with auto update mode");

wks.AutoSize(); // to resize worksheet. This method is very useful when data/text is longer than the width of cell

int nRows = wks.GetNumRows();
int nCols = wks.GetNumCols();

printf("The number of the rows in %s is: %d\n", wks.GetName(),nRows);
printf("The number of the columns in %s is: %d\n", wks.GetName(), nCols);

wks.SetLongName(wks.GetName());

//part that extract
string strCond = "a==null";
string strLTRunBeforeloop = "const null=0/0;range a=2";

vector<uint> vnRowIndices;
// assume the active wks has at least 5 columns
vector<uint> vnCols = {7};
int nn = wks.SelectRows(strCond, vnRowIndices, 0, -1, -1, NULL, NULL, NULL);
if(nn < 0)
out_str("User cancel");
else if(nn == 0)
out_str("no matching row");
else
{
// Worksheet wksfin(wp.Layers(0));

string strColName;
//Add a new column named "NewCol", if there already has a column named NewCol, will auto enumerate name with number and put name to strColName.
int iCol = wksfin.AddCol("LS", strColName);
// int iCol = wksfin.AddCol("LS"++, strColName);

if( iCol>=0 )
{
printf("New column added at index %d, name %s\n", iCol, strColName);
}

if(wksfin.CreateCopy(wksfin, CREATE_VISIBLE, DCTRL_COPY_DATA))
{
BOOL bRet = wks.Extract(wksfin, vnRowIndices, vnCols);
if(bRet)
printf("Extract selected rows to [%s]!%s.\n", wksfin.GetPage().GetName(), wksfin.GetName());
out_str("done"); //break;
}

}
}
}
else
out_str("failed to read ascii file");

}
}
}

Penn

China
644 Posts

Posted - 02/06/2013 :  12:45:56 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Maybe you can try the CopyTo method.

Penn
Go to Top of Page

lauransl

6 Posts

Posted - 02/06/2013 :  04:51:24 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks Penn, the problem with CopyTo is that when I use the example on ocwiki, (http://ocwiki.originlab.com/index.php?title=OriginC:Worksheet-CopyTo) it produces an error:
Error, Variable "CPYT_COPY_COLUMN_FORMAT" not declared

So, I included the oc_const.h header just in case this was missing but it didn't work either. I don't know how to declare the variable or if I am doing womething wrong.

The code already defines dwCntrl, so I guess I should define CPYT_COPY_COLUMN_FORMAT, but I don't know how.
DWORD dwCntrl = CPYT_COPY_COLUMN_FORMAT;
int nRet = wksSource.CopyTo(wksTarget, nC1, nC2, nR1, nR2, nDestC1, -1, dwCntrl);

Do you know how to fix this?

Thanks a lot!

Laura
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 02/06/2013 :  5:25:57 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Maybe your version of Origin is too old? I see in oc_const.h that CPYT_COPY_COLUMN_FORMAT was added on Nov 2008, which means it will require Origin 8 SR5 or later, so maybe you can check if you have the latest service release of Origin 8 or not, of better yet, if you can just upgrade to the latest version of Origin 9.

CP
Go to Top of Page

lauransl

6 Posts

Posted - 02/07/2013 :  05:31:20 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks CP, unfortunately to upgrade Origin there is too much bureaucracy to do and I don't know if we have the permission to do it.
So with my 8 SR0 version of origin, do you know a way to copy columns from one worksheet to another one? Thanks!
Laura
Go to Top of Page

Penn

China
644 Posts

Posted - 02/07/2013 :  9:20:58 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Try the Data_copy function, which should be available in Origin 8 SR0.

Penn
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