yamid

yamid

  • NA
  • 98
  • 0

Read the text data to a 3D array , THANKS to ALAN!

Nov 6 2008 1:17 PM

Thanks to Alan we could get the below code ran:
The Code reads the data file line by line and put it into a 3d array.

*facing the problem:

*"the input string is not in correct format"  upon reaching to temp[i * 5 + j] = double.Parse(numbers[j]);

*I changed the code as follows and it worked well.

double[,,] dArray = new double[76,140,150];
double[] temp = new double[76 * 140 * 150];
StringReader sr = new StringReader(filePath);
//string line = null;
string [] lines =File.ReadAllLines(path)

string[] numbers = null;
for (int i = 0; i < lines.Length; i++)
{
    //line = sr.ReadLine();
    numbers = lines[i].Split(' '); // split on space
    for(int j = 0; j < 5; j++)
    {
       temp[i * 5 + j] = double.Parse(numbers[j]);
    }
}  

sr.Close();

int count = 0;

for (int k = 0; k < 149; k++)
{
   for(int j = 0; j < 139; j++)
   {
      for(int i = 0; i < 75; i++)
      {
         dArray[i,j,k] = temp[count];
         count++;
      }
   }
}

temp = null;