written by: lore
email: lore at newty dot de
home: www.newty.de
last updated: 25.07.00
multithreaded "load ASCII-data-matrix from file" class
| void load(const char* filename) | Loads the data-matrix. This function can only be called once in the lifetime of an instance. This is easier to implement and costs only a minimum of additional resources. |
| void save(const char* filename) | Saves data-matrix to file. |
| inline int nDS() | Query number of rows. Inline function, i.g. always use it and do not copy the value to an own variable! |
| inline int dim() | Query number of columns. Inline function, i.g. always use it and do not copy the value to an own variable! |
Query of the i´th row. All counting starts with zero! Thus the first row is adressed by the index 0. |
|
| void calc() | Column-wise calculate statistical values which can be queried using getMeanVec(), getMinVec(), getMaxVec(), getRangeVec() and getDevVec(). |
| void randomize(const int n) | Interchange two random rows n-times. |
| void sort(const int col) | Sort rows according to the values of the specified column col. |
The following code is an excerpt from the project´s file main.cpp and shows how to use the class TAsciiData. There is only one function CmDataDemo() which is called when the user hits the menu-item Data-Demo.
/****************************************************/
//
// usage of "load ASCII data from file"-class
//
#include "asciidata.h"
/***********************************************************************************************************/
// load a file named "test.dat" and do some things with it ...
void mainWnd::CmDataDemo()
{
// a) create TAsciiData object
TAsciiData* data = new TAsciiData(this, font, IDD_DIALOG);
// b) read data - note: reading can cause an exception. thus encapsulate all in a try-block
try
{
data->load("test.dat"); // load data - this file must exist
data->calc(); // calculate statistical values
}
catch(TErrText err)
{
MessageBox(err.szErrText, "error loading test.dat", MB_OK | MB_ICONERROR);
}
// c) check success
if(data->dim() == 0 || data->nDS() == 0) // if data-matrix has zero rows or columns -> ...
{
data->release(); // ... release it again
return;
}
// d) do something with the data - note: in a try-block cause save() can throw an exception
try
{
// how to access the data - example: calculate the sum of each column
// IMPORTANT: all countig starts up with zero. thus the first row has the index 0.
double* sum = new double[data->dim()]; // allocate memory for the sums
for(int i=0;i<data->nDS();i++) // over all rows
{
const float* row = data->getRow(i); // get pointer to i-th´s row
for(int j=0;j<data->dim();j++) // over of columns of the actual row ...
sum[j]+=row[j]; // ... sum up
}
// display the sum of the first column and release 'sum'
char szText[256];
sprintf(szText, "The sum of the first column is: %f", sum[0]);
MessageBox(szText);
delete[] sum;
// how to sort, randomize and reorder
data->sort(0); // sort rows regarding column 0, i.g. the first column
data->save("sort.dat"); // save
data->randomize(4*data->nDS()); // randomize all rows
data->save("rand.dat"); // save
data->reorder(); // restore original order
data->save("original.dat"); // save
data->release(); // release
}
catch(TErrText err)
{
MessageBox(err.szErrText, "error:", MB_OK | MB_ICONERROR);
}
}
Download Example Project