Author |
Topic |
|
Fourierraum
Germany
3 Posts |
Posted - 07/02/2015 : 02:33:31 AM
|
Origin Pro 2015, 64bit, b9.2.214 Operating System: Win7
Hello, I have the following bit of Origin code which is supposed to import data, and then manipulate each column of data. Furthermore, I want to fit my data with a user defined function and read the obtained fitting parameters as well as errors on the parameters.
For now, I am stuck on the manipulation. This line vecA = (vecA - vecA[loc])*13.333333333333; gives the error Origin C Function Runtime Error, Vector element index greater than the upper bound. when trying to run.
If I comment out this one line, the code runs fine. However, in the for loop with counter variable ii I try to write back the vector data into the worksheet for using in the fitting, but this part does not actually write anything to the worksheet, the worksheet is empty after executing the program.
Any help is highly appreciated.
Thanks!
void import_file_with_specify_filter_ex1(int N) { uint nMin, nMax; int i, xx, yy, nRows; int nRet, nIndexLayerNew; char stringVariable[2]; string strPathNew, Worksheetname, WorksheetnameNew; vector vA,vB; int r1 = 0, c1 = 1, r2 = -1, c2 = -1; //These are for the Data Range below
//Some stuff to get the right sheet for importing Page wksPage = Project.Pages(); // Active Page //Get page book name string strPageName = wksPage.GetName(); //Get page active layer index int nIndexLayer = wksPage.Layers().GetIndex(); //End of the stuff // Get Origin sample folder //string strPath = GetAppPath(TRUE) + "Samples\\Signal Processing\\"; //Use own Data file: string strPath = "C:\Data\Tauon-Data\Test\data"; // specify .oif filter name //string strFilterName = "TR Data Files"; string strFilterName = "C:\\Program Files\\OriginLab\\Origin2015\\Filters\\ASCII.oif"; //Initialise the stuff for finding max and exporting Worksheet into vector Worksheet wks = Project.ActiveLayer(); Column colB(wks, 1); Column colA(wks,0);
vector<double> vecA = colA.GetDataObject(); vector<double> vecB = colB.GetDataObject();
double max = 0; int loc = 0; double val = 0;
//For finding the length of vectors int vecSize;
//Do File Import for(i = 0; i <= N; i++) { sprintf(stringVariable, "%d", i); strPathNew = strPath + stringVariable + ".dat"; //Add a worksheet to active workbook each time loop gets excecuted WorksheetnameNew = Worksheetname + stringVariable; int index = wksPage.AddLayer(WorksheetnameNew); nIndexLayerNew = nIndexLayer + i; //printf("%d",nIndexLayerNew); //This was just for testing purpose //printf("%s \n",strPathNew); //This was just for testing purpose int nRet = import_file(strPageName, nIndexLayerNew, strPathNew , strFilterName);
//Activate correct worksheet (I hope) didn't work without this line for some reason wks = Project.ActiveLayer(); //Initialise the datarange variable used to access the data in the worksheet DataRange dr; //Add the whole 2nd column to the datarange dr.Add("Y", wks, r1, c1, r2, c2); //Read in the data in 2nd column dr.GetData(&vB,1); //Find number of rows /* nRows = wks.GetNumRows(); //USELESS COMMAND! It just returns the number of rows in the worksheet, not the number of non-empty rows! printf("%d \n",nRows); */
vecSize = vecA.GetSize(); printf("vector size is %d \n", vecSize); //This was for testing
//Set size of worksheet appropriate to vector length wks.SetSize(vecSize,-1); for (int x=0; x<vecSize; x++) { if (vecB[x] > max) { max = vecB[x]; loc = x; val = vecA[x]; } printf("loc %d, val %f\n",loc,val); } //printf("Max %f, location %d,xAxis %f \n",max,loc,val); //This was for testing vecB = log(vecB); vecA = (vecA - vecA[loc])*13.333333333333;
for(i = 0; i < vecSize; i++) { printf("%f \n",vecA[i]); }
for(int ii = 0; i < vecSize; i++) { printf("Loop was excecuted %d times \n",i); //This was for testing int importResultA = wks.SetCell(ii,0,vecA[ii]); int importResultB = wks.SetCell(ii,1,vecB[ii]); printf("%d %d \n",importResultA,importResultB); } }
if ( 0 == nRet ) out_str("Success to import!"); else out_str("Failed to import!"); } |
|
SeanMao
China
288 Posts |
Posted - 07/02/2015 : 06:11:29 AM
|
Hi,
After debugging, following are the suggestions:
1. You need to declare vecA and vecB after data being imported:
int nRet = import_file(strPageName, nIndexLayerNew, strPath , strFilterName);
vector<double> vecA = colA.GetDataObject();
vector<double> vecB = colB.GetDataObject();
2. There is a typo in your code: i in for loop should be changed to ii instead:
for(int ii = 0; ii < vecSize; ii++)
{
printf("Loop was excecuted %d times \n",i); //This was for testing
int importResultA = wks.SetCell(ii,0,vecA[ii]);
int importResultB = wks.SetCell(ii,1,vecB[ii]);
printf("%d %d \n",importResultA,importResultB);
}
After the modification of these two points, we tested the function and it worked well.
Regards!
Sean
OriginLab Tech. Service |
|
|
Fourierraum
Germany
3 Posts |
Posted - 07/02/2015 : 1:21:18 PM
|
Thank you very much for your quick help! Modifying the program as you said made it work right. |
|
|
Fourierraum
Germany
3 Posts |
Posted - 07/02/2015 : 6:32:07 PM
|
One more question, this time about the fitting.
Now that I have successfully imported my data and written back to the worksheet, I am trying to fit the data with my user-defined function using the function nlfFit. As I stated in the original post, I need to read out the parameters of the fitting function after fitting and their errors.
How do I obtain these?
The example code on your website only returns a workbook with the fit function values after fitting, not the fit parameters.
Finally, when I am done with my code, how can I properly distribute it? The end-user should not go into code-builder and compile it every time. I have seen the tutorial here: http://www.originlab.com/doc/OriginC/guide/Distributing-Origin-C-Code but even this simple button did not work out right. It seems this tutorial is for an older version of Origin, as I could not follow the steps on there step-by-step because some dialogs were missing and I had to find the options myself.
Thank you for your help
EDIT: Also, I am trying to access the debug mode, following this tutorial http://wiki.originlab.com/~originla/howto/index.php?title=Tutorial:Debugging_Origin_C_Files_using_Code_Builder but the file DebugTutorial.c does not exist on my computer. |
Edited by - Fourierraum on 07/02/2015 8:34:05 PM |
|
|
SeanMao
China
288 Posts |
|
|
Topic |
|
|
|