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
 LabTalk Forum
 Import Text File and Process it to form new sheet
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Nroei

4 Posts

Posted - 07/26/2012 :  3:10:12 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 8.6
Operating System: Win 7

Hi All,

I've been using Origin for a year for simple graph plotting. I'm now hoping to use it for more serious data analysis and want to learn to use scripting to speed things up. I've been reading through the Origin UserGuide to learn, although this is proving a slow, dry way to learn. So any other good resources would be welcomed!

Anyway, my main question. Let's say I have a .txt document of the following format:

***** Data.txt **********
Header information:
param1 = a
param2 = a
...... etc....
param721 = hsdfdsfdf

# Begin TRACE A Data
P_0=-124.340000 , 19.525001 MHz
P_1=-124.020000 , 19.525004 MHz
P_549=-122.952000 , 19.526997 MHz

# Data Done

# Begin TRACE B Data
P_0=-116.516000 , 19.525001 MHz
P_1=-114.032000 , 19.525004 MHz
...

# Data Done
***** End of Data.txt **********

I want to import this data from the .txt file, strip the header information and put TRACE A data and TRACE B data into separate two column worksheets.

Firstly, I'm not sure whether to use LabTalk or OriginC for the purpose (or indeed more generally when each is best!).

My basic script idea is:
1) Read in text data from file
2) search for "#Begin TRACE A Data" and delete that line and all previous lines
3) search for first instance of "#Data Done" and delete that line and all following lines
4) Remove "P_*=" and " , " and " MHz" from the text where * is a single-character wildcard
5) Import the data into a new worksheet of the current workbook, making two columns
6) Loop, going to stage 2, but now processing for TRAC B

All advice / useful code would be greatly appreciated!

Thanks,
Nroei

greg

USA
1378 Posts

Posted - 07/27/2012 :  11:22:38 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
If you already have the data imported, then post-processing with LabTalk may be good enough, unless you have very large datasets.
If you need to use OriginC, then write the whole import and processing in one.

You would do well to begin playing around with methods of the string class many of which are inherited by LabTalk string variables.

Using Comma delimiter during import should get you two columns. Then run this script:

// BEGIN SCRIPT (create a filter to run after import in Import Wizard)
range ra1 = 1;
range ra2 = 2;
newsheet;
outputrow = 1;
loop(ii,1,ra1.GetSize())
{
str$ = ra1[ii]$;
if(str.GetLength() > 0)
{
if( str.GetToken(1)$ == "#")
{
if( str.GetToken(2)$ == "Begin" )
{
newsheet;
outputrow = 1;
}
}
else
{
temp$ = str.GetToken(2,'=')$;
col(1)[outputrow] = %(temp$);
str$ = ra2[ii]$;
temp$ = str.GetToken(1)$;
col(2)[outputrow] = %(temp$);
outputrow++;
}
}
}
// END SCRIPT

Here's an excellent source of information and code:
http://wiki.originlab.com/~originla/ltwiki/index.php?title=Category:LabTalk_Programming
Go to Top of Page

Nroei

4 Posts

Posted - 08/09/2012 :  5:05:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Sorry for the slow reply, but thanks for your help. Will try this now and use your references for help.

Thanks.
Go to Top of Page

Nroei

4 Posts

Posted - 08/09/2012 :  5:51:53 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by greg

Using Comma delimiter during import should get you two columns. Then run this script:




Ah, that's my first problem. Origin can't import the file due to the many lines of header text and the fact that it's essentially three datasets in one .txt file. That's why I wanted to use string processing to sort it out. Maybe I need to write more complex import code?

Can this be done?

NB, an example full file I want to process is: http://pastebin.com/NcrcDac4

Thanks again for your help!
Go to Top of Page

Nroei

4 Posts

Posted - 08/31/2012 :  4:50:04 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Bump, sorry.

Any suggestions? I've yet to find a solution.

Thanks.
Go to Top of Page

Drbobshepherd

USA
Posts

Posted - 09/06/2012 :  6:49:16 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
1. Import your datafile using an Ascii filter, using "=" as your delimiter. Then create a script that does the following:

2. Create a loop to find the line# where the header ends. Store the number in %B.

3. Then use the folowing commands to delete the header lines:

range ab=1[1]:2[%B];
del ab;
del col(A);

4. You could (a)write a loop program to search for and copy strings using space delimiters into new sheets and columns, or you could (b)export the data using the expASC X-function and then impASC the data into a new workbook with a space-delimeted ASCII filter. I think option b will be quicker. I offer the following commands as an example:

expASC path:="c:\MyDocuments\Temp\Temp.dat";
newbook;
impASC fname:="c:\MyDocuments\Temp\Temp.dat" Options.FileStruct.Delimiter:=7;

Delete cols B and D and you have your 2 cols of data. Now all you have to do is add new sheets (use the newsheet command), find the row numbers of the trace data sets, and use wrcopy to copy the traces into different sheets.

DrBobShepherd
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