I tried the solution Alan kindly provided to me( find the question and reply, below) , but every time there is an error say , the argument is not in correct format...
would you please tell me about?
To simplify my question, please look at below simplified example, ( you can find the previous question and codes, below section)
you can think of the 16 data like this:
I would like to put them in a 3d array with
4*2*2 dimension.
data file:
1 2 3 4 5 6 7 8
9 10 11 12 13
14 15 16
so,
for k=0 I have ,
1 2 3 4
5 6 7 8
and for k=1 I have
9 10 11 12
13 14 15 16
for example A[0,0,0]=1
A[1,0,0]=2
A[2,0,0]=3
A[3,0,0]=4
A[0,1,0]=5
A[1,1,0]=6
A[2,1,0]=7
A[3,1,0]=8
A[0,0,1]=9
A[1,0,1]=10
A[2,0,1]=11
A[3,0,1]=12
A[0,1,1]=13
A[1,1,1]=14
A[2,1,1]=15
A[3,1,1]=16
--PREVIOUS Question-----------------------------------------------------------------------------------------------------------------------------------------
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)
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();
yamidPosted Nov 6, 2008, 2:47 PM
Thanks alot for your real kind reply
*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;
AlanPosted Nov 6, 2008, 2:10 PM
If you're sure that the data doesn't contain anything that can't be parsed as a double and that there are always exactly 5 numbers on each line, then the problem could be caused by the presence of superfluous whitespace at the beginning or end of a line.
Try replacing this line:
numbers = line.Split(' '); // split on space
with this one:
numbers = line.Trim().Split(' '); // split on space
yamidPosted Nov 6, 2008, 1:10 PM
I got the error : the input string is not in correct format when reaching to :
temp[i * 5 + j] = double.Parse(numbers[j]);
AlanPosted Nov 6, 2008, 5:30 AM
I'd be inclined to load all the data into a temporary single dimensional array first and then transfer it to the 3D array. Like this (untested):
double[,,] dArray = new double[76,140,150];
double[] temp = new double[76 * 140 * 150];
StringReader sr = new StringReader(filePath);
string line = null;
string[] numbers = null;
StringReader sr = new StringReader(filePath);
for (int i = 0; i < 319200; i++)
{
line = sr.ReadLine();
numbers = line.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;
yamidPosted Nov 6, 2008, 4:32 AM
In fact at each line ( just in this case I need to Code) there are 5 number at each line and number of lines are319200.
It means I have an array of 76*150*140 cells. The data file is 319200 line but at each line 5 number exists.
Doy ou think we may be able to use arraylist....
AlanPosted Nov 6, 2008, 4:26 AM
My suggested code assumed there would always be 76 doubles on each line. It won't work if there are a variable number of doubles on each line which your simplified example is suggesting.
Can you confirm that this is the case and whether there is any pattern as to how many numbers will appear on the line, or whether it's more or less random.
The code also assumed that the doubles would be separated by commas but it appears that they're separated by spaces, though that's easily fixed.