Skip to main content
Cleaned up spelling, formatted code; added list-manipulation tag
Source Link
MarcoB
  • 67.7k
  • 18
  • 96
  • 198

I'm new here and with MathematicaMathematica, too. I want MathematicaMathematica to import data from a txt file (it hasthas one column of data). This entrysThese entries are z-values. From this I want to make a matrix - 508 entrysentries make one row, 308 rows in total. And And then I want it to be exportedexport it to a dat file.

Here is my code:

data = Import["D:\Data\Mathematica\Convert_data\rawdata.txt", "Table"]; Flatten[data]; carray = ConstantArray[1, {308, 508}]; For[j = 1, j < 309, j++, For[i = 1, i < 508, i++, carray[[j, i]] = data[[(j - 1)*508 + i]]]]; Export["test.dat", carray, "Table"]

data = Import["D:\\Data\\Mathematica\\Convert_data\\rawdata.txt", "Table"]; Flatten[data];
carray = ConstantArray[1, {308, 508}];
For[j = 1, j < 309, j++, 
  For[i = 1, i < 508, i++, carray[[j, i]] = data[[(j - 1)*508 + i]] ]
];
Export["test.dat", carray, "Table"]

It does nearly everything it is supposed to do. It creates a matrix as I want it to, but all the entrysentries are in curly braces. I think the problem is right at the beginning, when I import the data, all the entrysentries are in a curly brace, and all these braces are within a common brace. I tried to get rid of the single curly braces around each value with "Flatten", but it doesn't do anything.

The txt file I start with looks like this:

1.598669e+000
1.570571e+000
1.600274e+000
1.513271e+000
1.549999e+000
1.426268e+000
1.197772e+000 ....

1.598669e+000   
1.570571e+000   
1.600274e+000   
1.513271e+000   
1.549999e+000   
1.426268e+000   
1.197772e+000
....

It has many entrysentries, so I dontdon't show all of them here.

I'm new here and with Mathematica, too. I want Mathematica to import data from a txt file (it hast one column of data). This entrys are z-values. From this I want to make a matrix - 508 entrys make one row, 308 rows in total. And then I want it to be exported to a dat file.

Here is my code:

data = Import["D:\Data\Mathematica\Convert_data\rawdata.txt", "Table"]; Flatten[data]; carray = ConstantArray[1, {308, 508}]; For[j = 1, j < 309, j++, For[i = 1, i < 508, i++, carray[[j, i]] = data[[(j - 1)*508 + i]]]]; Export["test.dat", carray, "Table"]

It does nearly everything it is supposed to do. It creates a matrix as I want it to, but all the entrys are in curly braces. I think the problem is right at the beginning, when I import the data, all the entrys are in a curly brace, and all these braces are within a common brace. I tried to get rid of the single curly braces around each value with "Flatten", but it doesn't do anything.

The txt file I start with looks like this:

1.598669e+000
1.570571e+000
1.600274e+000
1.513271e+000
1.549999e+000
1.426268e+000
1.197772e+000 ....

It has many entrys, so I dont show all of them here.

I'm new here and with Mathematica, too. I want Mathematica to import data from a txt file (it has one column of data). These entries are z-values. From this I want to make a matrix - 508 entries make one row, 308 rows in total. And then I want to export it to a dat file.

Here is my code:

data = Import["D:\\Data\\Mathematica\\Convert_data\\rawdata.txt", "Table"]; Flatten[data];
carray = ConstantArray[1, {308, 508}];
For[j = 1, j < 309, j++, 
  For[i = 1, i < 508, i++, carray[[j, i]] = data[[(j - 1)*508 + i]] ]
];
Export["test.dat", carray, "Table"]

It does nearly everything it is supposed to do. It creates a matrix as I want it to, but all the entries are in curly braces. I think the problem is right at the beginning, when I import the data, all the entries are in a curly brace, and all these braces are within a common brace. I tried to get rid of the single curly braces around each value with "Flatten", but it doesn't do anything.

The txt file I start with looks like this:

1.598669e+000   
1.570571e+000   
1.600274e+000   
1.513271e+000   
1.549999e+000   
1.426268e+000   
1.197772e+000
....

It has many entries, so I don't show all of them here.

Source Link

Import a column of data, make a matrix from it and export it WITHOUT curly braces

I'm new here and with Mathematica, too. I want Mathematica to import data from a txt file (it hast one column of data). This entrys are z-values. From this I want to make a matrix - 508 entrys make one row, 308 rows in total. And then I want it to be exported to a dat file.

Here is my code:

data = Import["D:\Data\Mathematica\Convert_data\rawdata.txt", "Table"]; Flatten[data]; carray = ConstantArray[1, {308, 508}]; For[j = 1, j < 309, j++, For[i = 1, i < 508, i++, carray[[j, i]] = data[[(j - 1)*508 + i]]]]; Export["test.dat", carray, "Table"]

It does nearly everything it is supposed to do. It creates a matrix as I want it to, but all the entrys are in curly braces. I think the problem is right at the beginning, when I import the data, all the entrys are in a curly brace, and all these braces are within a common brace. I tried to get rid of the single curly braces around each value with "Flatten", but it doesn't do anything.

The txt file I start with looks like this:

1.598669e+000
1.570571e+000
1.600274e+000
1.513271e+000
1.549999e+000
1.426268e+000
1.197772e+000 ....

It has many entrys, so I dont show all of them here.