Author |
Topic  |
|
LifeDeath
USA
7 Posts |
Posted - 12/05/2020 : 05:49:13 AM
|
I open several dat files at a time each containing three columns of data. In this picture you can see what I mean: https://ibb.co/CBJBrRF So I automatically open three columns (from one file) and after each one is open I can use some script as you can see in this picture: https://ibb.co/vhvQ6s2 But on my previous picture you could see that I need absolute values of some columns, so I use the function abs(Col(Name)), and some columns should be X columns. I do it all manually, but can I script it? I've tried to look for commands and functions that I need but found nothing useful. Now I will describe figuratively the three step process that I want to script: After the data of three columns is added to a worksheet, I want to use this script (this is an abstract C-like script):
int N = LastColunmNumber; // I get the index of the last column at the current step
Make_X(Column(N - 2)); // the first column of the current iteration is now an X column
Column(N - 1).function = "abs(Col( Column(N - 1).name ))"; // Take absolute values of the penultimate column as its function
Column(N).function = "abs(Col( Column(N).name) )"; // Take absolute values of the last column as its function
And then the next file opens automatically and the script repeats, but now the N will be different. Sometimes I need to open dozens and even hundreds of files, so this script could really help. Can you please help me? And as a little bonus, maybe you know what I should add to that after-cycle script to make vertical bold lines after each three separate columns of data (as in the first picture). I will really appreciate your help. |
Edited by - LifeDeath on 12/05/2020 06:02:20 AM |
|
cpyang
USA
1406 Posts |
Posted - 12/05/2020 : 1:44:53 PM
|
I don't know which version of Origin you are using, but I wrote the code below with the current version of Origin 2021.
I use column formula even though can also just for the calculation directly. the benefit of using formula is the Fx row will show the formula used.
The code below allow user to pick a data file and import into the active workbook. It will set a group of 3 columns with 1st in the group to X and the remaining 2 to ABS of itself.
Data Connector is used because it will automatically detect column headers etc.
dlgfile g:=*.dat;//pick a file
wbook.dc.add("CSV");
wks.dc.source$ = fname$;
wks.dc.import();
wbook.dc.remove();//after this, can modify data
for(int ii = 1; ii<=wks.ncols;ii++) {
if(mod(ii, 3)==1)
wks.col$(ii).type=4;//X
else
csetvalue col:=wcol(ii) formula:="abs(this)";
}
CP
|
 |
|
LifeDeath
USA
7 Posts |
Posted - 12/06/2020 : 03:22:22 AM
|
Thank you Cpyang! Since I'm only beginning with Origin I'm very bad at it. The code that you wrote, should I open it in command window? Or should I save it through script window as .ogs? If I paste in the command window, it allows me to select only one dat file, and then it returns an error "#Command Error". If I try to execute it line by line, everything is fine until the third line wks.dc.source$ = fname$; where it returns the same error. I tried to use "Import multiple ASCII" button and choose several files (setting "Start new Columns" for "Multi-file (except 1st) Import Mode") and then I put last six lines from your code in "Script after all files imported" window, like this: https://ibb.co/vmv05rp
And it almost did everything as I need, the X columns are now fine, but the functions didn't paste, it seems, look here: https://ibb.co/stwxk3P I also showed and error saying "Undefined variable: this".
I'm sorry for being so stupid, but can you please tell me what I'm missing? I have OriginPro 2018. |
 |
|
cpyang
USA
1406 Posts |
Posted - 12/06/2020 : 07:22:53 AM
|
I am afraid your version of Origin does not support this code.
CP
|
 |
|
LifeDeath
USA
7 Posts |
Posted - 12/06/2020 : 07:29:59 AM
|
quote: Originally posted by cpyang
I am afraid your version of Origin does not support this code.
CP
Okay, bus is there a way to loop it after all columns have been put in a worksheet, where each fourth column gets X, and all others absolute of their own values, how to code this? I tried to use your code after all the columns have been open:
for(int ii = 1; ii<=wks.ncols;ii++) {
if(mod(ii, 3)==1)
wks.col$(ii).type=4;//X
else
csetvalue col:=wcol(ii) formula:="abs(this)";
}
It worked fine for titling X columns, but the functions didn't work, as I showed on the picture above. Here it is: https://ibb.co/CBJBrRF Can I change something in your piece of code that loops though all the columns so that it writes down the function correctly? |
Edited by - LifeDeath on 12/06/2020 07:31:05 AM |
 |
|
LifeDeath
USA
7 Posts |
Posted - 12/06/2020 : 11:29:54 AM
|
Is there a way to use something instead of "this" operator to put in a formula of absolute value of a current column? I think this is where the problem comes from because Origin does not recognize it. Maybe I should use formula:="abs(ii)" Is there an operator to return to a column's name? I thinks that's what I need. |
 |
|
cpyang
USA
1406 Posts |
Posted - 12/07/2020 : 10:48:36 AM
|
If "this" cannot be used, then just don't show the formula, like this
dlgfile g:=*.dat;//pick a file
wbook.dc.add("CSV");
wks.dc.source$ = fname$;
wks.dc.import();
wbook.dc.remove();//after this, can modify data
for(int ii = 1; ii<=wks.ncols;ii++) {
if(mod(ii, 3)==1)
wks.col$(ii).type=4;//X
else
wcol(ii)=abs(wcol(ii));
}
CP
|
 |
|
LifeDeath
USA
7 Posts |
Posted - 12/07/2020 : 2:34:59 PM
|
Oh God now it really works! Thank you a lot! You just saved me so much time. I really appreciate it. |
 |
|
|
Topic  |
|
|
|