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
 Sort Files 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

a.abc.b35

175 Posts

Posted - 01/09/2011 :  12:22:20 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
I am importing multiple ASCII files from a folder to worksheet using origin C. All the ASCII data are in X vs Y column format (2 columns in each file,X column is same for all).I am importing the data is a single worksheet with single X column and multiple Y columns.
Below is the code I am using:
.......................................................
void importfolder()
{
string strPath = BrowseGetPath(); // input folder path
printf("Folder Path: ");// outputs the path of the input folder
out_str(strPath); // outputs the path of the input folder

StringArray saFiles; // string vector which will store all the file names in the folder with given extension
FindFiles(saFiles, strPath, "chi");// Finds files with ext 'chi' in strpath and stores file names in saFiles
if( saFiles.GetSize() < 1 ) // checks if there is no file present in the folder
return;

ASCIMP ascimp; // structure for ASCII type data import. see online if detail is needed.
AscImpReadFileStruct(strPath + saFiles[0], &ascimp);// scans and analyzes the given file (1st argument)
//to gather info for ASCII import into worksheet.stores in ascimp.


Worksheet wks; // define a worksheet

wks.Create(); // create a wks for the new data
wks.ImportASCII(strPath + saFiles[0], ascimp); // Imports ASCII data from the file (1st argument)
// and stores in ascimp.

ascimp.iMode = ASCIMP_MODE_APPEND_COLS; //
ascimp.iPartial = ASCIMP_PARTIAL_YES;
ascimp.iPartialC1 = 1; // imports from 2nd column
ascimp.iPartialC2 = 1; // imports upto 2nd column

for(int i = 1; i < saFiles.GetSize(); i++ )
wks.ImportASCII(strPath + saFiles, ascimp);

}

.............................................................
I got this code from another forum in origin c !:)
....
Now the problem is the following: ASCII files are numbered as 1,2,....9,10,11,12,...19,20,...29,30....101...etc.And I want them to be imported numerically. But what the code does is that it imports as 1,10,100,101,11,...19,2,21,....etc. So I figured that the files needs to sorted after they are stored in the string array 'saFiles'. For this I wanted to use Array.sort() as in C-programming. But it does not compile and gives the following error :
[i]Error, Member function vector::sort not defined or does not have matching prototy

....
Can anyone help me solve this problem ?

AB

a.abc.b35

175 Posts

Posted - 01/09/2011 :  12:45:21 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Than I searched a bit more in the origin wiki and used saFiles.Sort().
But it does not solve it,though the program compiles now !

Could any one help quickly please...

AB
Go to Top of Page

Penn

China
644 Posts

Posted - 01/12/2011 :  03:53:16 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi AB,

StringArray is synonym for string vector. So you can try the Sort method.

Penn
Go to Top of Page

rlewis

Canada
253 Posts

Posted - 01/12/2011 :  4:39:59 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
If your files are numbered in order MyFile_1, MyFile_2, MyFile_3 ... MyFile_108 .... then your problem is quite easy to solve ...

Try something like ...



void DoStuff()
{
	vector<string> vctString;
	vctString.RemoveAll();
	// Create a Numerically  indexed string array of MyFile_250, Myfile_249, MyFile_248 ..... MyFile_1
	// sorted in reverse order ...
	for(int i=250;i>=1;i--)
	{
		string strTemp="MyFile_"+("%d",i);
		vctString.Add(strTemp);
	}
	if(SortByIndices(vctString)==true)
	{
		// The Array FilesNames should now be sorted in ascending order and one can 
		// process the Contents in order with 
		for(i=0;i<vctString.GetSize();i++)
		{
			out_str(vctString[i]);
		}
	}
}


bool SortByIndices(vector<string> &vctMyNames)
{
	WorksheetPage wP;
	// Create a temporary WorkBook
	if(wP.Create("origin.otw",CREATE_TEMP)==true)
	{
		Worksheet Wks(wP.Layers(0));
		if(Wks.IsValid()==true)
		{
			Wks.SetSize(vctMyNames.GetSize(),2);
			Column wCol1(Wks,0);
			Column wCol2(Wks,1);
			if(wCol1.IsValid()==false || wCol2.IsValid()==false)
			{
				return (false);
			}
			// Set Column 1 as numeric and Column 2 as Text ...
			if(wCol1.SetFormat(OKCOLTYPE_NUMERIC)==false || wCol2.SetFormat(OKCOLTYPE_TEXT)==false)
			{
				return (false);
			}
			// Place the string Array into Column 2;
			if(wCol2.PutStringArray(vctMyNames)==false)
			{
				return (false);
			}
			vector<int> vctIndices;
			vctIndices.RemoveAll();
			// Parse the String Array to get the numerical Indices of the file names
			// and store these indices in a array vctIndices
			for(int i=0;i<vctMyNames.GetSize();i++)
			{
				string strTemp1=vctMyNames[i];
				string strIndex=strTemp1.GetToken(1,'_');
				vctIndices.Add(atoi(strIndex));
			}
			Dataset dS;
			if(dS.Attach(Wks,0)==false)
			{
				return (false);
			}
			// Place the contents of the array vctIndices int column 1 of the Worksheet Wks
			dS.SetSize(vctIndices.GetSize());
			dS=vctIndices;
			// Sort the Worksheet as per the contents of Column 1 in ascending order;
			if(Wks.Sort()==false)
			{
				return (false);
			}
			// Copy the sorted array back to vctMyNames
			if(wCol2.GetStringArray(vctMyNames)==true)
			{
				return (true);
			}
		}
	}
	return(false);;
}

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