Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Because the data in the text file has one column, importing using the "Table" format produces a nested N × 1 array, where N is the number of lines in the file. Instead, use the "List" import format, and you will get a one-dimensional array:

Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"]

should give {1.59867, 1.57057, 1.60027, 1.51327, 1.55, 1.42627, 1.19777, ...}. The rest of your code should then work as expected, except the condition in the For loop over i should be i < 509.

But it's almost always better to use built-in functions, rather than loopsrather than loops. You can use the function Partition to split your list into a nested 308 × 508 array:

Export["test.dat", Partition[Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"], 508], "Table"];

Here's an example using ImportString and ExportString instead:

ExportString[Partition[ImportString["1.598669e+000
    1.570571e+000
    1.600274e+000
    1.513271e+000
    1.549999e+000
    1.426268e+000
    1.197772e+000
    1.197772e+001", "List"], 4], "Table"]

Because the data in the text file has one column, importing using the "Table" format produces a nested N × 1 array, where N is the number of lines in the file. Instead, use the "List" import format, and you will get a one-dimensional array:

Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"]

should give {1.59867, 1.57057, 1.60027, 1.51327, 1.55, 1.42627, 1.19777, ...}. The rest of your code should then work as expected, except the condition in the For loop over i should be i < 509.

But it's almost always better to use built-in functions, rather than loops. You can use the function Partition to split your list into a nested 308 × 508 array:

Export["test.dat", Partition[Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"], 508], "Table"];

Here's an example using ImportString and ExportString instead:

ExportString[Partition[ImportString["1.598669e+000
    1.570571e+000
    1.600274e+000
    1.513271e+000
    1.549999e+000
    1.426268e+000
    1.197772e+000
    1.197772e+001", "List"], 4], "Table"]

Because the data in the text file has one column, importing using the "Table" format produces a nested N × 1 array, where N is the number of lines in the file. Instead, use the "List" import format, and you will get a one-dimensional array:

Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"]

should give {1.59867, 1.57057, 1.60027, 1.51327, 1.55, 1.42627, 1.19777, ...}. The rest of your code should then work as expected, except the condition in the For loop over i should be i < 509.

But it's almost always better to use built-in functions, rather than loops. You can use the function Partition to split your list into a nested 308 × 508 array:

Export["test.dat", Partition[Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"], 508], "Table"];

Here's an example using ImportString and ExportString instead:

ExportString[Partition[ImportString["1.598669e+000
    1.570571e+000
    1.600274e+000
    1.513271e+000
    1.549999e+000
    1.426268e+000
    1.197772e+000
    1.197772e+001", "List"], 4], "Table"]
Source Link

Because the data in the text file has one column, importing using the "Table" format produces a nested N × 1 array, where N is the number of lines in the file. Instead, use the "List" import format, and you will get a one-dimensional array:

Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"]

should give {1.59867, 1.57057, 1.60027, 1.51327, 1.55, 1.42627, 1.19777, ...}. The rest of your code should then work as expected, except the condition in the For loop over i should be i < 509.

But it's almost always better to use built-in functions, rather than loops. You can use the function Partition to split your list into a nested 308 × 508 array:

Export["test.dat", Partition[Import["D:/Data/Mathematica/Convert_data/rawdata.txt", "List"], 508], "Table"];

Here's an example using ImportString and ExportString instead:

ExportString[Partition[ImportString["1.598669e+000
    1.570571e+000
    1.600274e+000
    1.513271e+000
    1.549999e+000
    1.426268e+000
    1.197772e+000
    1.197772e+001", "List"], 4], "Table"]