Data Reader model element

A data reader encapsulates an OSP DataFile class to read a text file with a double[][] array of data.

The file must consist of a number of character-delimited text lines, each containing one of:

Notice that the separator character must be the same all through the file (a blank space or a tab are excellent candidates), and that the name of entries in all line must match.

Usage

To add a data reader element to your model, drag the icon element to the list of your model elements and set the "File to read" field to a file in your hard disk (under your simulation directory) or link it to a String model variable (as in %myModelString%).

Use the buttons to the right of the field for easier selection.

A data reader element does not read your data file until you instruct it to do so (in any suitable part) in your model code.

A call to the element method double[][] readData() will attempt to read the file and return the double[][] data array, if successful, or null if it failed to do so.

A call to the method String[] getColumnNames(String _firstColumnName) will return the String[] array of column names for the last data read. The parameter _firstColumnName is an optional column name that will be added at the beginning of the array of column names. If set to null, it will be ignored. (Using this parameter is very helpful for array panels which display the row number, for instance.)

A call to the method String getDataName() will return the name of the last data read, if specified in the file.

Example of use

double[][] dataArray = dataReader.readData();
if (dataArray==null) {
  _println("Could not read data from "+dataReader.getFilename());
  return;
  }
String dataName = dataReader.getDataName();
String[] colNames = dataReader.getColumnNames("Row #");
// Now, do whatever you want with this data...