Author |
Topic |
|
Clairekun
Germany
175 Posts |
Posted - 05/29/2014 : 04:40:44 AM
|
Origin Ver. and Service Release (Select Help-->About Origin): 9 Operating System: Windows 7
Hello,
First of all, sorry for the long post. I have several data files which I would like to import into new columns or new books, depending on each case:
1. When I measure the same parameter several times for one sample, data files have the same name and are numbered like: ABC1, ABC2, ABC3. 2. When I measure another sample, file name changes.
In the end, my files look like:
ABC1 ABC2 ABC3 DEF1 DEF2 (...)
I would like to import to new columns those which have the same file name, and to new books those with different file names. In the end, it would be like:
Book 1: XY columns for ABC1, ABC2 and ABC3 Book 2: XY columns for DEF1 and DEF2
These would be XY XY books. Some time ago I asked the same question for XYYY workbooks and this was the reply, in case it can help:
dlgfile gr:=*.asc mu:=1;
loop(ii,1,fname.GetNumTokens(CRLF))
{
strFile$ = fname.GetToken(ii,CRLF)$;
strPart$ = strFile.GetFileName()$;
strPart$ = strPart.Left(1)$;
if(exist(%(strPart$))==2)
{
win -a %(strPart$);
newsheet;
impasc fname:=strFile$ options.FileStruct.NumericSeparator:=0 options.Names.FNameToBk:=0;
}
else
{
newbook name:=%(strPart$) option:=lsname;
impasc fname:=%(strFile$) options.FileStruct.NumericSeparator:=0 options.Names.FNameToBk:=0;
}
} Afterwards, I would like to plot those recently imported files. I asked how to do something like this here, where the reply was:
string fns;
dlgfile g:=*.txt m:=1 f:=fns$ t:="Select CV data" init:="F:\" ;
int n = fns.GetNumTokens(CRLF); //Gets the info from those files
loop(ii,1,n)
{
file$ = fns.GetToken(ii,CRLF)$;
impasc fname:=file$ // IMPORT JUST ONE FILE
//(impasc options here)
// NOW PLOT
plotxy iy:=(1,2) plot:=200 ogl:=[<new>];
} // Next file
I am trying really hard to understand the whole code. I would like to know:
1. Does win -a %(strPart$) require both conditions to be met?
2. What if(exist(%(strPart$))==2) means (mostly the ==2 part).
3. Why should newsheet be there if what I want are new columns? Should I still use newsheet and specify new columns in impasc options? What difference would it make if it was newbook?
4. How to modify if and else. I believe the loop for plotting needs to be in both commands. I am familiar with impasc options, so I could perform changes here if needed.
5. About plotting, is there any way to specify the different XY ranges? I can't set them to ((1,2),(3,4), (...)) since there aren't always the same number of XY sets.
Thank you kindly. I know it was a lot to read, so I really appreciate you taking the time for it. |
Edited by - Clairekun on 05/29/2014 04:48:16 AM |
|
lkb0221
China
497 Posts |
Posted - 05/29/2014 : 09:46:26 AM
|
Hi,
To import to XYXY books:
//Script Start dlgfile gr:=*.asc mu:=1; loop(ii,1,fname.GetNumTokens(CRLF)) { strFile$ = fname.GetToken(ii,CRLF)$; strPart$ = strFile.GetFileName()$; strPart$ = strPart.Left(1)$; if(exist(%(strPart$))==2) { win -a %(strPart$); impasc fname:=strFile$ options.sparklines:=0 options.ImpMode:=1 options.FileStruct.NumericSeparator:=0 options.Names.FNameToBk:=0 options.Names.FNameToSht:=0; } else { newbook name:=%(strPart$) option:=lsname; wks.name$ = %(strPart$); impasc fname:=%(strFile$) options.sparklines:=0 options.FileStruct.NumericSeparator:=0 options.Names.FNameToBk:=0; } wks.col$(wks.ncols - 1).type = 4; wks.col$(wks.ncols).type = 1; } //Script End
You want to plot all XYXYs in the same workbook to the same graph window or separate ones? |
Edited by - lkb0221 on 05/29/2014 09:48:29 AM |
|
|
lkb0221
China
497 Posts |
Posted - 05/29/2014 : 09:58:01 AM
|
1). win -a %(strPart$) means to activate the window named by the string variable strPart$.
2). exist(%(strPart$)) means to test if something called by the string variable strPart$ exists. If it is a workbook/worksheet, will return value 2.
3). See the above script for how to import to new columns. (options.ImpMode:=1)
4). Not sure what do you mean...
5). You can use the following script: // Script Start worksheet -s 0 0 0 0; // highlight all the columns in the current worksheet worksheet -p 200; // plot the highlighted columns as a grouped line plot. // Script End |
Edited by - lkb0221 on 05/29/2014 09:58:54 AM |
|
|
Clairekun
Germany
175 Posts |
Posted - 05/29/2014 : 10:21:46 AM
|
Thank you. I tried it and I get the following error:
Error: value 4 is missing a variable name
If I remove those 2 lines, all files get imported to new columns, although correctly.
Question 4 referred mainly to plot code but, now that I read it, I'm not that sure it makes sense. I want to plot in the same graph those XY sets which belong to the same workbook, and in different graphs those belonging to different ones. |
|
|
lkb0221
China
497 Posts |
Posted - 05/29/2014 : 11:37:16 AM
|
Hi,
The error is caused by those two wks.col.type commands? I cannot reproduce the error. Maybe you can send me some data files to run the test.
Or, you can change the column type in the plotting part as following:
//Script Start //Make sure only wanted workbooks are left in the current folder window -c Book1; //delete workbook "Book1"
//Loop through all workbooks in the current folder document -ef W { for (int ii = 1; (ii+1) <= wks.ncols; ii = ii+2) // Change columns to XYXY { wks.col$(ii).type = 4; wks.col$(ii+1).type = 1; } worksheet -s 0 0 0 0; // highlight all the columns in the current worksheet worksheet -p 200; // plot the highlighted columns as a grouped line plot. } //Script End |
Edited by - lkb0221 on 05/29/2014 11:37:56 AM |
|
|
Clairekun
Germany
175 Posts |
Posted - 06/02/2014 : 06:57:58 AM
|
Ok I created 3 data files, called Way14, Way15 and Way20, setting stpart.left to 4 letters.
1. Origin creates an empty 2-column workbook called Way1 and the script stops, showing the following error:
Error: value import\Way14.txt is missing a variable name #Command Error!
2. If I rerun the script on that same created workbook, only the first file is imported and another error appears:
#User Abort!
The same file keeps importing to new columns if I rerun the script.
The script I used is:
//Script Start dlgfile gr:=*.txt mu:=1; loop(ii,1,fname.GetNumTokens(CRLF)) { strFile$ = fname.GetToken(ii,CRLF)$; strPart$ = strFile.GetFileName()$; strPart$ = strPart.Left(4)$; if(exist(%(strPart$))==2) { win -a %(strPart$); impasc fname:=strFile$ options.sparklines:=0 options.ImpMode:=1 options.FileStruct.NumericSeparator:=0 options.Names.FNameToBk:=0 options.Names.FNameToSht:=0; } else { newbook name:=%(strPart$) option:=lsname; wks.name$ = %(strPart$); impasc fname:=%(strFile$) options.sparklines:=0 options.FileStruct.NumericSeparator:=0 options.Names.FNameToBk:=0; }
//Loop through all workbooks in the current folder document -ef W { for (int ii = 1; (ii+1) <= wks.ncols; ii = ii+2) // Change columns to XYXY { wks.col$(ii).type = 4; wks.col$(ii+1).type = 1; } worksheet -s 0 0 0 0; // highlight all the columns in the current worksheet worksheet -p 200; // plot the highlighted columns as a grouped line plot. } //Script End
If I omit the looping part of the script:
- Result 1 if there is no workbook with the short name "Way1" - All Way1x files are imported to new columns if there is a workbook with the short name "Way1" (either created by the first run or manually). There is a new empty workbook created, with the name Way2. Error:
Error: value import\Way20.txt is missing a variable name #Command Error!
- Running the script for a third time, once Way1 is created, data files imported and there is a blank workbook called Way2, results in the import of way20.txt correclty.
|
Edited by - Clairekun on 06/02/2014 08:11:01 AM |
|
|
lkb0221
China
497 Posts |
Posted - 06/02/2014 : 09:42:01 AM
|
Hi,
I think the problem is you forget to close the import loop. Simply add a } before plotting loop (doc loop) should fix the problem.
Zheng OriginLab |
|
|
Clairekun
Germany
175 Posts |
Posted - 06/02/2014 : 09:44:28 AM
|
I noticed right after I wrote this, but the results are the same :( |
|
|
lkb0221
China
497 Posts |
Posted - 06/02/2014 : 09:52:29 AM
|
Hi,
Would you mind sending these three txt files to <tech@originlab.com> so I can have a try? I cannot reproduce the error with simple fake txt files I created with those names...
Zheng OriginLab |
|
|
Clairekun
Germany
175 Posts |
Posted - 06/04/2014 : 10:22:00 AM
|
Thank you for your help, this is now solved.
In case anyone has the same problem:
Make sure the folder path your data files are stored in does not contain any spaces, or strFile.GetFileName()$ will go crazy and the script will stop, showing errors.
For example:
- C:\Users\User1\My data\sample.txt ---> Error will appear ("data\filename.txt is missing a variable name") - C:\Users\User1\Mydata\sample.txt ---> Script runs smoothly |
Edited by - Clairekun on 06/04/2014 10:23:08 AM |
|
|
cdrozdowski111
USA
247 Posts |
Posted - 06/04/2014 : 7:19:08 PM
|
Try this:
You can use directories with spaces in the name!
In your calls to the impasc function, wrap %(strFile$) in double quotes:
impasc fname:="%(strFile$)" options.FileStruct.NumericSeparator:=0 options.Names.FNameToBk:=0;
I copy/pasted your script (including Zheng's } fix). Then I created a directory with three fake data files:
C:\My Test\Way14.txt
C:\My Test\Way15.txt
C:\My Test\Way20.txt
The script didn't blow up after adding the double quotes. |
Edited by - cdrozdowski111 on 06/04/2014 7:23:17 PM |
|
|
Clairekun
Germany
175 Posts |
Posted - 06/05/2014 : 02:01:25 AM
|
Oh wow, thank you! That was really useful!
I believe I'll save the script with double quotes. I assume it will work if there are no spaces, too, so it's better like this.
Thanks again!
One last thing: if I wanted to import csv files, how could I do it? Because if I change impasc to impcsv and set firstmode and mode to 1, it still imports same-name files to new books. |
Edited by - Clairekun on 06/05/2014 02:22:52 AM |
|
|
cdrozdowski111
USA
247 Posts |
Posted - 06/05/2014 : 06:06:22 AM
|
Here is one option to import both *.txt and *.csv files. It can import both in the same script run.
1) Changedlgfile gr:=*.txt mu:=1; to dlgfile gr:="*.txt; *.csv;" mu:=1;
2) For BOTH of your calls to the impasc function, add the following parameters to the call:
options.Miscellaneous.Qualifier:=1 options.Miscellaneous.RemoveQuotes:=1
Based on a quick test of simple, numeric data-only CSV files, it appears to work correctly. |
|
|
Clairekun
Germany
175 Posts |
Posted - 06/05/2014 : 06:20:10 AM
|
My csv files are not data-only, so I'm afraid I will need to have 2 different scripts. In my csv, data starts at line 18, so the import script would be:
if ( exist(%(strPart$)) == 2) { win -a %(strPart$); impcsv fname:=strFile$ options.FirstMode:=1 options.Mode:=1 options.Partial.Partial:=1 options.Partial.Firstsample:=18; } else { newbook name:=%(strPart$) option:=lsname; wks.name$ = %(strPart$); impcsv fname:="%(strFile$)" options.Partial.Partial:=1 options.Partial.Firstsample:=18; }
This imports all data files correctly, but in different workbooks. |
|
|
|
Topic |
|