I have a text data file including Double type data , as below;
x x x x
x x x x
.
.
.
x x
How to read this data file line-by line from left to right and put them in a 3D array(in C#). The order of numbers in data file is in the way that first i changes from 0-75, then j to 139 and then k to149.
it means the filling procedure is like this
reading to array from (0,0,0) to (75,0,0) then (0,1,0) to (75,1,0)... then(0,139,0)to(75,139,0)
(0,0,1) to(75,0,1) then(0,1,1)to(75,1,1)... then(0,139,1)to(75,139,1)
.
.
.
(0,0,149)to(75,0,149) then (0,1,149)to(75,1,149) ...then(0,139,149)to(75,139,149)
Thanks alot for your Help
AlanPosted Oct 31, 2008, 11:33 AM
Assuming it's a comma-separated file, then you should be able to read it into a 3D array something like this.
I haven't added any error handling and so, if the file isn't exactly how you expect it to be, you'll have a problem:
double[,,] dArray = new double[76,140,150];
string line = null;
string[] numbers = null;
StringReader sr = new StringReader(filePath);
for (int k = 0; k < 149; k++)
{
for(int j = 0; j < 139; j++)
{
line = sr.ReadLine();
numbers = line.Split(',');
for(int i = 0; i < 75; i++)
{
dArray[i,j,k] = double.Parse(numbers[i]);
}
}
}
sr.Close();