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
 Forum for Origin C
 How to read data into worksheet
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

tony_lincoln

USA
Posts

Posted - 09/08/2006 :  11:12:25 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Dear friends,
I am learning Origin C now and really wonder how to read txt file into worksheet. Then I could do the curve fitting.
In my txt file, there are two float columns, one is x and one is y.
THanks.
Tony

Mike Buess

USA
3037 Posts

Posted - 09/09/2006 :  12:42:47 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Tony,

The following code imports, plots and fits a *.txt file. Import and plot code is based on an example from here. Fitting code is based on what you posted here.
void simple_import_and_fit()
{
// Bring up file dialog for user to select the file.
// Replace *.txt with your desired extension as needed.
string strFile = GetOpenBox("*.txt");
if( strFile.IsEmpty() )
{
out_str("No file was selected!");
return;
}

// Create a new worksheet
Worksheet wks;
// If you wish to use a custom worksheet template, then specify the name of
// your template in place of Origin.OTW
wks.Create("Origin.OTW");
// Import the file into the worksheet using the current ASCIMP
// (Import ASCII Options) stored in the worksheet.
// You can edit the ASCIMP options from the GUI and save it back to
// the worksheet and then save the worksheet as a custom template.
// You can then create a new instance of your custom template and use it
// to import the data next time.
BOOL bRet = wks.ImportASCII(strFile);
if( !bRet )
{
out_str("Failed to import file!");
// Destroy newly created worksheet
wks.Destroy();
return;
}

// Create a default graph and plot all Y columns in the first layer
// as a grouped plot.
GraphPage gpg;
// If you wish to use a custom graph template, then specify the name
// of your template in place of Origin.OTP
gpg.Create("Origin.OTP");
// Point to the first layer in the graph
// If you use a custom template that has multiple layers, you can
// then declare separate layer objects for each layer and move
// data plots into various layers as desired
GraphLayer gly = gpg.Layers(0);
// plot the y column (column 2)
Curve crv(wks, 1);
gly.AddPlot(crv, IDM_PLOT_LINE);
// rescale
gly.Rescale();
using NLSF = LabTalk.NLSF; // Point to the NLSF object
NLSF.Init(); // Initialize the fitter
NLSF.Func$ = "gaussamp"; // Assign fitting function
NLSF.FitData$ = crv.GetName(); // Assign dataset name
NLSF.Execute("parainit"); // Perform automatic parameter initialization
NLSF.Fit(100); // Perform fit - up to 100 iterations
NLSF.PasteParams("p"); // Paste fit results to graph
}


Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 09/09/2006 08:15:58 AM
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 09/09/2006 :  1:36:41 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You'll find a useful example project in your program folder...

Samples\Programming\Automation\Automation.opj (Origin 7.0)
Samples\Programming\Automation\AutomationExample.opj (Origin 7.5)

Note that it's VERY useful to indicate your Origin version in the starting post of a topic.

Mike Buess
Origin WebRing Member
Go to Top of Page

tony_lincoln

USA
Posts

Posted - 09/09/2006 :  10:40:02 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Great, it works in a perfect way. Thanks a lot, Mike!
Tony
Go to Top of Page

tony_lincoln

USA
Posts

Posted - 09/09/2006 :  11:34:28 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Mike,

Thanks a lot. But still one question here --- How to judge the goodness of fit.
The the codes that you wrote, its like:
NLSF.Fit(100);
Of course I can change it into
NLSF.Fit(1000000);
But the problem is, how do I know if it fitts well, and what is the goodness of fit.
Thanks a lot.

Tony
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 09/10/2006 :  07:57:49 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Best fit has the smallest chi-square value. It would be pointless to use nlsf.fit(1000000) because fitting stops when when chi-square ceases to change.

Mike Buess
Origin WebRing Member
Go to Top of Page

tony_lincoln

USA
Posts

Posted - 09/10/2006 :  12:10:06 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Great, Thanks a lot, Mike.
Tony
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