Author |
Topic  |
|
bg-phys
USA
Posts |
Posted - 08/07/2006 : 4:13:13 PM
|
Origin Version: 7.5 SR5 Operating System: Win XP SP2
Hi,
I'm a C programmer trying to learn Origin C.
I have a C program that will read binary data from a binary data file (*.dat). After the program runs I have a two-dimensional array of type long int that is filled with the data from the *.dat file.
long data[a][b]; ... fopen(...); fread(...); for(i=0,...) { for(j=0,...) { data[i][j] = ...; } }
At this point I'd like to have the columns of my data[][] array become the columns of a worksheet named "MyWorksheet" in origin.
How do I interface with Origin in order to accomplish this?
Thanks in advance,
bg-phys
P.S.
I've read that two dimensional arrays are not supported in Origin C and I should use a matrix instead. I'm not quite sure how to do that, but it does compile as a 2-D array.
Also, "long" isn't highlighted in blue by the Code Builder's syntax highlighter so I suspect that it isn't a supported data type.
Edited by - bg-phys on 08/07/2006 4:14:51 PM |
|
easwar
USA
1965 Posts |
Posted - 08/07/2006 : 4:31:08 PM
|
Hi bg-phys,
The data type long is same as int and that is why long is not changed to blue - see Supported Basic and Derived Data Types section of Introduction to Programming->Programming in Origin C under Programming Guide chapter of the programming help file for more info.
As for putting data into the worskheet, Origin C has access to Origin objects such as datasets in worksheets, so why not write the data directly into worksheet as you read from the file, with code such as:
void binary_data_import() { WorksheetPage wpg; wpg.Create("Origin"); Worksheet wks = wpg.Layers(0); Dataset dsX(wks, 0); Dataset dsY(wks, 1); // The following is a dummy loop // Replace with your loop of reading binary file for(int i = 1; i <10; i++) { // say the x,y values you read from file are in vars nx, ny int nX = i *10; // some dummy value for this example int nY = i * 20; // some dummy value for this example dsX.Add(nX); // add to x column dsY.Add(nY); // add to y column } }
Also, if your binary file has a very simple structure, try the Import Wizard where you can define a filter to import the file. The filter can then be programmatically used to import the file if desired.
Easwar OriginLab
Edited by - easwar on 08/07/2006 4:33:08 PM |
 |
|
cpyang
USA
1406 Posts |
Posted - 08/07/2006 : 8:19:20 PM
|
Your best bet would be to read the data into a matrix, like
matrix<int> mat;
int nn; ff.Read(&nn, 4);
mat[i][j] = nn;
after all read into mat, you can put into a worksheet using
mat.CopyTo(wks);
CP
|
 |
|
bg-phys
USA
Posts |
Posted - 08/11/2006 : 3:39:04 PM
|
Thank you. Your suggestions were very helpful. Unfortunately, I'm still having some problems.
I want to import the data into two seperate Origin Worksheets. One titled "Left" and one titled "Right". Each worksheet should contain several columns of y-axis data and no x-axis data: Y Y Y Y Y Y Y ... Y Y Y Y Y Y Y ... ...
I tried:
void binary_data_import(string filepath) { //ERROR: general operation failure long i, j, k, l; int numColumns = 100; int numRows = 500; /* Create a worksheet for Left data */ WorksheetPage wpgLeft; wpgLeft.Create("Origin"); Worksheet wksLeft = wpgLeft.Layers(0); LT_execute("win -r %H Left;"); //Rename window to "Left"
/* Create a worksheet for Right data */ WorksheetPage wpgRight; wpgRight.Create("Origin"); Worksheet wksRight = wpgRight.Layers(0); LT_execute("win -r %H Right;"); //Rename window to "Right"
/* Four commands to change the worksheet dimensions */ LT_execute("wksLeft!wks.nCols = numColumns;"); LT_execute("wksRight!wks.nCols = numColumns;"); LT_execute("wksLeft!wks.nRows = numRows;"); LT_execute("wksRight!wks.nRows = numRows;");
l=0; /* loop over all columns */ for (i=0; i<numColumns; i++) { //ERROR: general operation failure Dataset dsLeft(wksLeft, i); //WARNING: Data type mismatch Dataset dsRight(wksRight, i); //WARNING: Data type mismatch /* loop over all rows */ for (j=0; j<numRows; j++) { //ERROR: general operation failure dsLeft.Add(i); //ERROR: general operation failure dsRight.Add(j); l++; } } }
By my figuring I should end put with:
Left: Right:
0 1 2 3 4 5 ... 0 0 0 0 0 0 ... 0 1 2 3 4 5 ... 1 1 1 1 1 1 ... 0 1 2 3 4 5 ... 2 2 2 2 2 2 ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
But instead I end up worksheets that look like this:
Left: Right:
0 1 0 0 0 1 1 1 0 1 2 2 . . . . . . . . . . . .
And I get "general operation failures" and "Data type mismatches" on the lines indicated above.
It seems that the program will extend the number of rows as necessary, but not the number of columns. I can take out the four grouped LT_execute statements with no change in the program behavior.
Edited by - bg-phys on 08/11/2006 3:53:04 PM |
 |
|
rlewis
Canada
253 Posts |
Posted - 08/11/2006 : 5:05:10 PM
|
The following code segment will achieve what you expected using other worksheet and dataset properties. I hope this helps ... bool binary_data_import() { long i, j, k, l; int numCols = 100; int numRows = 500; bool Flag; string strWksLeft,strWksRight; string PathToTemplate=GetAppPath(true); PathToTemplate+="Origin.otw"; strWksLeft=CreateVisibleWorkSheet(PathToTemplate,false); strWksRight=CreateVisibleWorkSheet(PathToTemplate,false); Worksheet WksLeft, WksRight; if(WksLeft.Attach(strWksLeft)==true && WksRight.Attach(strWksRight)==true) { if(WksLeft.GetPage().Rename("Left")==1 && WksRight.GetPage().Rename("Right")==1) { if(WksLeft.SetSize(numRows,numCols,true)==true && WksRight.SetSize(numRows,numCols,true)==true) { Dataset dS; for(int i=0;i<numCols;i++) { if(dS.Attach(WksLeft,i)==false) return (false); dS=i; if(dS.Attach(WksRight,i)==false) return (false); dS.Data(0,(numRows-1),1); } return (true); } } } return (false); }
string CreateVisibleWorkSheet(string PathToTemplate, bool Flag) { if(PathToTemplate.IsFile()) { Worksheet Wks; int vFlag; if(Flag==true) { vFlag=CREATE_VISIBLE; } else { vFlag=CREATE_HIDDEN; } if (Wks.Create(PathToTemplate, vFlag)) { return (Wks.GetPage().GetName()); } } return (""); }
|
 |
|
|
Topic  |
|
|
|