Beispiel #1
0
        public static DoubleTable FromString(string tableString, char delimiter = '\t')
        {
            StringReader  reader     = new StringReader(tableString);
            string        sDelimiter = delimiter.ToString();
            List <string> temp       = new List <string>();
            string        line       = reader.ReadLine();

            while (line != null)
            {
                temp.Add(line);
                line = reader.ReadLine();
            }
            reader.Close();

            if (temp.Count == 0)
            {
                return(new DoubleTable(0, 0));
            }

            int         nCols = temp[0].Count(c => { return(c == delimiter); }) + 1;
            int         nRows = temp.Count - 1;
            DoubleTable table = new DoubleTable(nRows, nCols);

            table.columnNames = temp[0].Split(delimiter).ToList();
            table.columnIndex.Clear();
            for (int iCol = 0; iCol < nCols; iCol++)
            {
                table.columnIndex.Add(table.columnNames[iCol], iCol);
            }

            string[] buffer;
            for (int i = 0; i < table.nRows; i++)
            {
                buffer = temp[i + 1].Split(delimiter);
                // check to see if it is blank
                if (!IsTrailingBlankRow(buffer))
                {
                    for (int j = 0; j < buffer.Length; j++)
                    {
                        if (!double.TryParse(buffer[j], out table.data[i, j]))
                        {
                            table.data[i, j] = double.NaN;
                        }
                    }
                }
                else
                {
                    table.nRows--;
                }
            }
            return(table);
        }
Beispiel #2
0
        public static DoubleTable ReadFromFile(string FileName, char delimiterAsChar = '\t')
        {
            string tableString;

            try
            {
                tableString = File.ReadAllText(FileName);
            }
            catch (Exception e)
            {
                throw new Exception("Error reading table.", e);
            }

            DoubleTable table = FromString(tableString, delimiterAsChar);

            return(table);
        }