T O P I C R E V I E W |
Clairekun |
Posted - 03/18/2020 : 2:01:16 PM Origin Ver. and Service Release (Select Help-->About Origin): 2018b Operating System: Windows 10
Hello,
Let me first say that I know little to nothing about coding, but I have used and written several labtalk scripts throughout the years so I am slightly familiar with it. I am trying to import data from a series of files, using ImpASC function. The long name and units are in the same line, following the pattern:
Sig1 Time (min) Sig2 Temperature (°C) Sig3 Weight (mg)
I have tried every google search I can think of to find information as to how to use and define such one-line patterns, but I simply can't find anything. I'm aware it may be in plain sight, but my lack of knowledge of coding simply makes me fail to recognise it.
I would also need to know if there was any way to extract text from one of the main header lines and assign it to a string. I need to make a new column with a mathematical operation using weight and a fixed value from one of the header lines, whose pattern is similar to the one stated above (text + value + unit) and is different for every file.
Sorry if this has been asked before, I tried to find similar problems from other users to no avail. |
8 L A T E S T R E P L I E S (Newest First) |
Clairekun |
Posted - 04/09/2020 : 05:02:50 AM quote: Originally posted by bhaiterabhai
Choose File: Import Single ASCII or File: Import Multiple ASCII. Select your ASCII file(s) and make sure that Show Options Dialog is checked. This opens the impASC dialog, where you can choose your data import handling options. Under the Header Lines branch, set Composite Header Line No. to 1 - 9 or type a number into the box. Make a selection from the Composite Pattern list. If you select <auto>, the import code will scan for recognized delimiters and will parse the line accordingly.
This belongs to my other post, not here, but thank you. However, I wanted to do it programmatically, since the files are stored in many different folders and Labtalk would be faster, in this case. I will make sure to use your method whenever I have a simpler folder structure :). |
bhaiterabhai |
Posted - 04/09/2020 : 01:43:47 AM If this check box is selected, the Main Header Lines and Number of subheader Lines controls will become uneditable and Origin will seek to determine these two values automatically. If the auto results is not good(you can quickly verify this in the preview window at the bottom of the page), you can specify these options manually by clearing this check box.
[url=https://get-shareit.com]shareit[/url] [url=https://get-vidmateapk.com]vidmate[/url] |
bhaiterabhai |
Posted - 04/09/2020 : 01:35:46 AM Choose File: Import Single ASCII or File: Import Multiple ASCII. Select your ASCII file(s) and make sure that Show Options Dialog is checked. This opens the impASC dialog, where you can choose your data import handling options. Under the Header Lines branch, set Composite Header Line No. to 1 - 9 or type a number into the box. Make a selection from the Composite Pattern list. If you select <auto>, the import code will scan for recognized delimiters and will parse the line accordingly.
|
YimingChen |
Posted - 03/19/2020 : 2:43:52 PM Good to know it worked.
string strH = __HEADER$; ---call to Headers lines
int id = 33; ---goes down to integer 33
for (int i = 1; i <=7; i++) ---applies whatever is between brackets to the next 7 integers, starting from 33
string strColHeader = strH.GetToken(id, CRLF)$; ---gets the 7 integers from position 33 that are separated by a carriage return (which would define integers as lines) This is to retrieve the 33th line, id += 1 is just to read the next line.
string strLongName = strColHeader.GetToken(1, '(')$; ---reads up to (. Does 1 mean something like "until"?Same GetToken function as that in above line to split the text by separator "(" and get the first partition.
string strLongNameTrimed = strLongName.Right(strLongName.Len()-5)$; ---deletes first 5 characters (Sig# ).
string strUnit = strColHeader.GetToken(2, '(')$;---gets text from ( onwards. Does 2 mean something like "from"? Get the second partition of the text divided by "(".
strUnit.TrimRight(")"); ---deletes ).
wcol(i)[L]$ = strLongNameTrimed$;
wcol(i)[U]$ = strUnit$; ---These two I can understand
id += 1; ---This, however, I cannot. Explained above
string strW = strH.GetToken(12, CRLF)$; ---Get 12th item from header (defined above) that is separated by a carriage return.
double dWfixed = value(strW.GetToken(2, TAN)$);--- Shouldn't it be TAB? Create a temporary variable: get the value of the 2nd item that is separated by a TAB. It still worked, though... why?This is my typo, should be TAB. Here tab and space is treated equally, seems by design.
wks.addCol();
wcol(wks.ncols)[L]$ = % Weight;
wcol(wks.ncols) = col("Weight")*100/dWfixed; ---This, I understand.
James |
Clairekun |
Posted - 03/19/2020 : 2:09:53 PM Hello,
Worked like a charm, thank you so much!. Let me try to understand what the code did, so that I can, perhaps, modify it for similar needs in the future.
string strH = __HEADER$; ---call to Headers lines
int id = 33; ---goes down to integer 33
for (int i = 1; i <=7; i++) ---applies whatever is between brackets to the next 7 integers, starting from 33
string strColHeader = strH.GetToken(id, CRLF)$; ---gets the 7 integers from position 33 that are separated by a carriage return (which would define integers as lines)
string strLongName = strColHeader.GetToken(1, '(')$; ---reads up to (. Does 1 mean something like "until"?
string strLongNameTrimed = strLongName.Right(strLongName.Len()-5)$; ---deletes first 5 characters (Sig# ).
string strUnit = strColHeader.GetToken(2, '(')$;---gets text from ( onwards. Does 2 mean something like "from"?
strUnit.TrimRight(")"); ---deletes ).
wcol(i)[L]$ = strLongNameTrimed$;
wcol(i)[U]$ = strUnit$; ---These two I can understand
id += 1; ---This, however, I cannot.
string strW = strH.GetToken(12, CRLF)$; ---Get 12th item from header (defined above) that is separated by a carriage return.
double dWfixed = value(strW.GetToken(2, TAN)$);--- Shouldn't it be TAB? Create a temporary variable: get the value of the 2nd item that is separated by a TAB. It still worked, though... why?
wks.addCol();
wcol(wks.ncols)[L]$ = % Weight;
wcol(wks.ncols) = col("Weight")*100/dWfixed; ---This, I understand.
Sorry for taking so much of your time, and thanks again.
|
YimingChen |
Posted - 03/19/2020 : 10:36:04 AM Hi,
Please try the following script to import your data file. Change the file path before running.
impASC fname:=C:\s43Q600II.txt options.headers.AutoSubHeaderLines:=0 options.headers.MainHeaderLines:=45;
string strH = __HEADER$;
int id = 33;
for (int i = 1; i <=7; i++)
{
string strColHeader = strH.GetToken(id, CRLF)$;
string strLongName = strColHeader.GetToken(1, '(')$;
string strLongNameTrimed = strLongName.Right(strLongName.Len()-5)$;
string strUnit = strColHeader.GetToken(2, '(')$;
strUnit.TrimRight(")");
wcol(i)[L]$ = strLongNameTrimed$;
wcol(i)[U]$ = strUnit$;
id += 1;
}
string strW = strH.GetToken(12, CRLF)$;
double dWfixed = value(strW.GetToken(2, TAN)$);
wks.addCol();
wcol(wks.ncols)[L]$ = % Weight;
wcol(wks.ncols) = col("Weight")*100/dWfixed;
James |
Clairekun |
Posted - 03/19/2020 : 05:12:22 AM Hello,
I sent the requested file. If you look at it, there are 45 header lines. Long names and units are found at lines 33-39, starting from column 9 (text before that is to be omitted).
Line 35: Sig3 Weight (mg) ----> Text + tab + long name + units
For clarification, I used red font for the desired bits. I also need to generate a new column , %Weight, where its formula is 100 x Weight column/weight fixed value. This fixed value can be found at line 12, columns from 9 to 16. The position is constant throughout all files.
Line 12: Size 59.0140 mg ----> Text + tab + value + units
It is important to note that header lines are not being imported.
I can define column long names and units after import by manually typing them (col(A)[L]$, col(A)[U]$...), which I have no problem with, although I would like to know if there was a way to include that under impasc Options.Headers.Oneline Expression, option stated at https://www.originlab.com/doc/X-Function/ref/impASC. I assume I need to replace "Oneline Expression" with a previously defined stirng.
This string would need to refer to extracted text from lines 33-39, where Long Name starts at column 9 and ends before "(" character, and units start at "(" character until the end of the line.
As for the fixed value, a similiar principle applies: text from line 12 and columns 9-16 is to be extracted and assigned to a string.
I sincerely have no idea of how to begin to achieve this, or if it is even achievable.
Thank you.
|
YimingChen |
Posted - 03/18/2020 : 5:21:06 PM Hi,
If after importing, the column LongName is in the structure like Time(min), we can write script to parse the LongName and put min to Unit column header cell. That could be one option.
Yes, you can parse the mainheader lines after the import and save the wanted information in worksheet. Please check the script below where the headerline string is saved in __HEADER$ string variable. https://www.originlab.com/doc/LabTalk/examples/general-Import#Controlling_Header_and_Long_Name
Please send a sample data file to <tech@originlab.com> that we can check for you.
James |
|
|