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
 Origin Forum
 zeroing
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

juannim

USA
Posts

Posted - 02/15/2007 :  10:19:43 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5 pro
Operating System: xp

I have worksheets, each of which consists of 46 columns. The first column needs to stay in the worksheet, but I don't want to anything further with it. I want to zero the data in each of the remaining columns individually (subtract the minimum value in that column from each data point in that column), then export the 45 zeroed columns to a single CSV ascii file. I'd love to be able to write the script myself, but I am not there yet. Jonathan

Mike Buess

USA
3037 Posts

Posted - 02/15/2007 :  12:36:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Jonathan,

The LabTalk and Origin C solutions given below are nearly identical so take your pick.
// LabTalk method

loop(i,2,wks.ncols)
{
sum(%(%H,i));
%(%H,i) -= sum.min; // subtract minimum value of column
};
wo -s 0 0 0 0; // select all columns
wks.colSel(1,0); // deselect first column
wks.export.cntrl=8; // export selected columns
wks.export.separator$=","; // comma separated
getsavename *.csv; // get file name (%A)
save -wh %H %A; // export to %A


// Origin C method
void subtract_and_export()
{
Worksheet wks = Project.ActiveLayer();
for(int i=1;i<wks.GetNumCols();i++)
{
Dataset ds(wks,i);
ds -= min(ds); // subtract minimum value of column
}
wks.LT_execute("wo -s 0 0 0 0");
wks.LT_execute("wks.colSel(1,0)");
wks.GetPage().Refresh();
string strFile = GetSaveAsBox("*.csv"); // get file name
if( !strFile.IsEmpty() )
wks.ExportASCII(strFile, WKS_EXPORT_SELECTED, ',');
}


Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 02/15/2007 12:40:30 PM
Go to Top of Page

juannim

USA
Posts

Posted - 02/15/2007 :  3:18:01 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Mike,
I tried inserting the C function as the main so that I zero all the columns and export with a single click, but it doesn't seem to work. here's my screensave. What am I doing wrong?
Go to Top of Page

Laurie

USA
404 Posts

Posted - 02/15/2007 :  3:54:07 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The Origin C method requires that you copy the code to a .C file and then first Build (compile and link), so that the function is available to you from LabTalk.

The main section of your OGS file, would then just call this function: subtract_and_export()

You could also programmatically compile and link the C file with the run.loadOC command. The main section of the Custom.ogs file would then be the following (assuming name of .C file is SubtractAndExport.c):


if( run.loadOC("SubtractAndExport.c") )
{
type -b "SubtractAndExport.c failed to load.";
return 1; // error
}
subtract_and_export;
return 0; // success

Since you're just starting out, you might want to choose the LabTalk method as there are fewer steps.

Laurie
OriginLab Technical Support
Go to Top of Page

juannim

USA
Posts

Posted - 03/23/2007 :  01:29:51 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Mike,
I tried running that script in a worksheet script window, but no love. It exports a file, but doesn't zero. What am I doing wrong? J
Go to Top of Page

zachary_origin

China
Posts

Posted - 03/23/2007 :  02:10:32 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Mike's script works fine for me...

Copy the LabTalk script to Script Window, highlight all the lines and press Enter...

Zachary
OriginLab Technical Services.

Go to Top of Page

Deanna

China
Posts

Posted - 03/23/2007 :  02:23:36 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Jonathan.

I tried Mike's code. It does "zero" all the columns except the first one, and sucessfully exports the zerod columns as a CSV file.

Would you please try the following procedure?
1. Make sure that the workbook that has the data is active.

2. From Origin menu, choose Tools: Worksheet Script to open the Worksheet Script Window.

3. Copy the following script into the Worksheet Script Window.

loop(i,2,wks.ncols)
{
sum(%(%H,i));
%(%H,i) -= sum.min; // subtract minimum value of column
};
wo -s 0 0 0 0; // select all columns
wks.colSel(1,0); // deselect first column
wks.export.cntrl=8; // export selected columns
wks.export.separator$=","; // comma separated
getsavename *.csv; // get file name (%A)
save -wh %H %A; // export to %A

4. Click Do It button.

5. The "Save As" dialog box should be opened. Browse to the folder where you want to save the CSV file and name the CSV file in the File Name textbox. Click Save.

This should be able to save the zeroed columns. If problem persists, please tell us.

Deanna
OriginLab Technical Services
Go to Top of Page

juannim

USA
Posts

Posted - 03/23/2007 :  10:47:22 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I still can't get it to subtract out the minimum value in each column. What am I doing wrong? I followed Deanna's directions. Jonathan
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 03/23/2007 :  11:26:24 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Perhaps your definition of "minimum value" differs from ours. We take it to mean the smallest value in the column. Do you mean something else? In any case, please paste your script to your reply.

Mike Buess
Origin WebRing Member
Go to Top of Page

juannim

USA
Posts

Posted - 03/23/2007 :  11:38:55 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
here is the script that I am pasting:
loop(i,2,wks.ncols)
{
sum(%(%H,i));
%(%H,i) -= sum.min; // subtract minimum value of column
};
wo -s 0 0 0 0; // select all columns
wks.colSel(1,0); // deselect first column
wks.export.cntrl=8; // export selected columns
wks.export.separator$=","; // comma separated
getsavename *.csv; // get file name (%A)
save -wh %H %A; // export to %A

I want to subtract the smallest value in each column from every value in that column. So, I would expect to see a zero at some point in each column where the minimum value subtracts from itself, yes? The only possible screw-up that I can think of is that I don't have the worksheet "active". I assume, however, that if I have some of the columns in that worksheet highlighted, then it is active. Jonathan
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 03/23/2007 :  11:49:45 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
I assume, however, that if I have some of the columns in that worksheet highlighted, then it is active.
Not necessarily. In the figure below Data3 is active even though columns are selected in Data2. The active worksheet is always on top. You said earlier that the script exports a file... Have you looked at that file to determine which worksheet was exported?



Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 03/23/2007 11:50:28 AM
Go to Top of Page

juannim

USA
Posts

Posted - 03/23/2007 :  11:51:32 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I only have one worksheet in the project, so it must be active, yes? Jonathan
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 03/23/2007 :  12:04:25 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
I want to subtract the smallest value in each column from every value in that column.
I've tried the same script several times and it always does exactly that.
quote:
I only have one worksheet in the project, so it must be active, yes?
A graph or matrix can also be active. The top window is active. Assuming that is the correct worksheet window I can't think of a reason for the script to fail. You might look at this...

http://www.originlab.com/www/support/resultstech.aspx?ID=1080&language=English

Mike Buess
Origin WebRing Member
Go to Top of Page

juannim

USA
Posts

Posted - 03/23/2007 :  12:24:20 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I have tried it on 3 different computers. None of them does anything to the data beyond removing the first column and saving a csv file. Here is a screensave of the original data, and the output (the excel file) Jonathan

Edited by - juannim on 03/23/2007 12:25:55 PM
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 03/23/2007 :  2:23:55 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Sorry, I forgot that Deanna suggested running the script from the Worksheet Script dialog. That sometimes behaves oddly and I can reproduce your results when I run the script from that dialog. So run it from the script window instead. Make sure the worksheet is on top, select all lines of the script and press enter. Alternately, you can copy the script to Custom.ogs like you tried before and run it from the Custom Routine button.

...In order to run the script successfully from the Worksheet Script dialog the loop command must be on one line.

loop(i,2,wks.ncols) {sum(%(%H,i)); %(%H,i) -= sum.min};

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 03/23/2007 2:30:22 PM
Go to Top of Page

juannim

USA
Posts

Posted - 03/23/2007 :  2:28:09 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
That did it. Thanks so much. Jonathan
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