3.6.1 Import ASCII
Version Info
Minimum Origin Version Required: Origin 8 SR0
Description
To import ASCII data, the method Datasheet::IimportASCII can be used. It allows to customize importation.
How to setup header lines to column lables after import
This example is to show how to import ascii data and how to setup sub header lines to column lables including uer defined parameter labels.
void ImportASCII_Ex1()
{
ASCIMP ai;
string strFile = GetOpenBox("*.dat"); // or "*.txt"
if(strFile.IsEmpty())
return;// user cancel
if(AscImpReadFileStruct(strFile, &ai)==0)
{
ai.iAutoSubHeaderLines = 0; //set auto detected to false since will set sub header to col label by below codes
ai.iSubHeaderLines = 4; // 1. LongName 2. Units 3. Expanded Discription(User defined) 4. Type Indication(User defined)
// Notice when iAutoSubHeaderLines is 0, the index of ai.nLongName, ai.nUnits and ai.nFirstUserParams are begin from Main Header
ai.nLongNames = ai.iHeaderLines;
ai.nUnits = ai.iHeaderLines + 1;
ai.nFirstUserParams = ai.iHeaderLines + 2;
ai.nNumUserParams = 2;
ai.iMaxLabels = 0; // Not set any header line to Comments label
Worksheet wks;
wks.Create();
if(0 == wks.ImportASCII(strFile, ai))
{
// Set user parameter labels to specified names
Grid grid;
grid.Attach(wks);
vector<string> vsUserLabels = {"Expanded Discription", "Type Indication"};
grid.SetUserDefinedLabelNames(vsUserLabels);
wks.AutoSize(); // to resize worksheet. This method is very useful when data/text is longer than the width of cell
}
}
else
out_str("failed to read ascii file");
}
To show how to do partial import
void ImportASCII_Ex2()
{
Worksheet wks = Project.ActiveLayer();
if(!wks)
{
out_str("no acitve workbook window to import data");
return;
}
ASCIMP ai;
string strFile = GetAppPath(1) + "Samples\\Curve Fitting\\Step01.dat";
if(!strFile.IsFile())
{
strFile = GetOpenBox("*.dat"); // or "*.txt"
if(strFile.IsEmpty())
return;// user cancel
}
if(AscImpReadFileStruct(strFile, &ai)==0)
{
ai.iMode = ASCIMP_MODE_APPEND_COLS; // append imported data to this worksheet
ai.iRenameCols = 0; // 0 to keep default column name
ai.iPartial = 1; // 1 to specify partial import
ai.iPartialC1 = 1; // import from 2nd column (0 offset)
ai.iPartialC2 = 4; // set the last column, so here just import 4 columns
ai.iPartialR1 = 0; // set the import row from
ai.iPartialR2 = 9; // set the import row to
int nRet = wks.ImportASCII(strFile, ai);
out_int("nRet = ", nRet);
if(0 == nRet)
wks.AutoSize(); // to resize worksheet. This method is very useful when data/text is longer than the width of cell
}
else
out_str("failed to read ascii file");
}
How to import a data file by using semicolon as delimiter
Firstly, run the following LabTalk script to load and compile the fu_utils.c file.
run.LoadOC(Originlab\fu_utils.c);
Then compile the Origin C code below.
#include <..\originlab\fu_utils.h>
void ImportASCII_Ex3()
{
string fileName = GetOpenBox("*.txt"); // txt file browser
if( !fileName.IsFile())
return;
// create a new worksheet
Worksheet wks;
wks.Create();
// ascii import
ASCIMP ai;
initASCIMP(ai); // initialize
ai.iAutoSubHeaderLines = 1; // auto detect subheader line
ai.iDelimited = 1; // use delimiter
ai.iDelimiter = ASCIMP_DELIM_DEMICOLON; // semicolon as delimiter
int nret = AscImpReadFileStruct(fileName, &ai, AIRF_USE_ASCIMP);
if(0 == nret )
{
// import
wks.ImportASCII(fileName, ai);
}
}
How to Remove Quotation Marks from csv Files
This sample shows how to import a csv file and trim all the quotes around text entries. Please note that fu_utils.c is needed to use ascii import functions. You can run the following LabTalk script to load and compile it.
run.LoadOC(Originlab\fu_utils.c);
Then compile the Origin C code below.
#include <..\originlab\fu_utils.h>
void ImportASCII_Ex4()
{
string fileName = GetOpenBox("*.csv"); // csv file browser
if( !fileName.IsFile())
return;
// create a new worksheet
Worksheet wks;
wks.Create();
// ascii import
ASCIMP ai;
initASCIMP(ai); // initialize
ai.iAutoSubHeaderLines = 1; // auto detect subheader line
ai.iDelimited = 1; // use delimiter
ai.iDelimiter = ASCIMP_DELIM_COMMA; // comma as delimiter
ai.iNonnumeric = 1; // NONNUMERIC_READ_AS_MISSING
// special quote symbol and remove it when import
ai.cQuote = '\"';
ai.flags |= AI_FLAG_REMOVE_QUOTES;
int nret = AscImpReadFileStruct(fileName, &ai, AIRF_USE_ASCIMP);
if(0 == nret )
{
// import
wks.ImportASCII(fileName, ai);
}
}
|