Adding Data From File to 2D Double Array

Typechecker

Member
Joined
Mar 24, 2013
Messages
5
Programming Experience
Beginner
Hi,

I am new in C#
I have a file and it includes N x N 2d array.I have counted number of rows and cols and created array[row][col]
I should add the numbers from file to 2D Double Array as it is.

1.555 2.455 1.0000
2.446 123.66 47.00
3.25 2.00 2.34

How can I carry the data from file to 2 dimensional double array?Given data above is just sampleI need them in double format because i will compute them after placed in array.

Need Help.

Thanks.
 
If you don't know ahead of time what the dimensions of the array will be then you need to read the data in order to determine that, so the two are intertwined. Here's what I would do:
var rows = new List<double[]>();
var columnCount = -1;

using (var reader = new StreamReader("file path here"))
{
    while (!reader.EndOfStream)
    {
        var textValues = reader.ReadLine().Split();

        if (columnCount != -1 && textValues.Length != columnCount)
        {
            // A row has been detected that is not the same length as the others so abort.
        }

        columnCount = textValues.Length;

        var numbers = new double[columnCount];
        double number;

        for (int col = 0; col < columnCount; col++)
        {
            if (!double.TryParse(textValues[col], out number))
            {
                // An invalid value was detected so abort.
            }

            numbers[col] = number;
        }

        rows.Add(numbers);
    }
}

// The file has been read successfully so create the matrix.
var rowCount = rows.Count;
var matrix = new double[rowCount, columnCount];

for (int row = 0; row < rowCount; row++)
{
    for (int col = 0; col < columnCount; col++)
    {
        matrix[row, col] = rows[row][col];
    }
}
I haven't included a test to make sure that the file did contain some rows. I'll leave that to you.
 
If you don't know ahead of time what the dimensions of the array will be then you need to read the data in order to determine that, so the two are intertwined. Here's what I would do
I haven't included a test to make sure that the file did contain some rows. I'll leave that to you.

Hey,Thanks for your reply.

Before I see your post,I implemented a code.It does not take dots.For Example;1.500 is taken as 1500.Problem is same in your code.I splitted dots as comma but it is just usefull to show as output and i can not compute the numbers with comma.I need dots to compute numbers.

Here is my code.

I test this code.It works fine,some additions needed.For example,If there is an enter space after last numbered line,it creates 1 more row in array and adds zero to it's locations and also I should implement a condition to look if there is irregularity between rows as you said.Also the number parsed or not.But these are not problem now.Problem is dots.Takes 1.5 as 15 and when I compute it,I take wrong result.I have numbers with dots in string array but when I convert to double,I lose dots.By the way i counted rows and cols in a class.I will embed this code also into class but I am testing now.
Do u have any suggestion for dots?
C#:
                double[,] datadouble = new double[NewFile.GetRow(), NewFile.GetCol()];
                string[,] datastring = new string[NewFile.GetRow(), NewFile.GetCol()];
                string[] list1 = new string[NewFile.GetRow() * NewFile.GetCol()];
                list1 = System.IO.File.ReadAllLines(openFileDialog1 .FileName );
                char[] whitespace = new char[] {' '};
                int i = 0; int j = 0;

                //Takes  data into 2d string array
                 foreach (string l in list1)
                 {
                     j = 0;
                     string my = l;
                     my = Regex.Replace(my, @"\s+", " ");//Converts all multispaces to one space
                     string[] words = my.Split(whitespace );

                     foreach (string s in words)
                     {
                         if (s != "")//This is avoidance from new lines.Normally a space shown when newline comes.I couldn't handle it using regex
                         {
                             datastring[i, j] = s;
                             j++;
                             listBox1.Items.Add(s);
                         }
                     }
                    i++;
                 }
                   //Converts 2d string array to double.
               for (int op = 0; op < NewFile.GetRow(); op++)
               {
                   for (int op1 = 0; op1 < NewFile.GetCol(); op1++)
                   {
                          double doubledata;
                          double.TryParse(datastring[op, op1], out doubledata);
                          datadouble[op, op1]  = doubledata;
                   }
               }
 
Last edited:
I'm done.

double.TryParse (datastring[op,op1],NumberStyles.AllowDecimalPoint,CultureInfo .InvariantCulture ,out doubledata );

Thank you so much.
 
Back
Top Bottom