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
 Error: Vector Index greater than upper bound
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Fourierraum

Germany
3 Posts

Posted - 07/02/2015 :  02:33:31 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
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  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

Fourierraum

Germany
3 Posts

Posted - 07/02/2015 :  1:21:18 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you very much for your quick help!
Modifying the program as you said made it work right.
Go to Top of Page

Fourierraum

Germany
3 Posts

Posted - 07/02/2015 :  6:32:07 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

SeanMao

China
288 Posts

Posted - 07/02/2015 :  10:51:41 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

In the example in page linked below, the content in function show_params() is what you are looking for to output fitted parameters:

http://www.originlab.com/doc/OriginC/ref/NLFitSession-SetParamValues

and also the errors if you use syntax "int GetFitResultsParams(vector& vParams, vector& vErrors)".

I had no problem when I followed the tutorial http://www.originlab.com/doc/OriginC/guide/Distributing-Origin-C-Code and run in my Origin 2015 SR2, I am not sure which dialog you are referring to is missing.

For debugging, the link you are using is an old one, you can check out this page instead:

http://www.originlab.com/doc/Tutorials/OC-CodeBuilder

and more reference can be found on this page:

http://www.originlab.com/doc/CodeBuild/Compiling-And-Debugging-Origin-C-Files

Regards!

Sean
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