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:


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

  • $\begingroup$ It would be helpful if you could post your data, for example at a site like pastebin, so others can download it and test possible solutions. $\endgroup$
    – dionys
    Jul 28, 2015 at 14:12
  • $\begingroup$ Thank you for that suggestion, dionys. And here is the file: s000.tinyupload.com/index.php?file_id=46674322808024643081 $\endgroup$
    – Torsten
    Jul 28, 2015 at 14:24
  • 1
    $\begingroup$ FYI your Flatten[data] does nothing. (It creates a flat version of data and discards it). You would need data=Flatten[data] to do what you probably though you were doing. $\endgroup$
    – george2079
    Jul 28, 2015 at 14:54

2 Answers 2


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:

    1.197772e+001", "List"], 4], "Table"]
  • $\begingroup$ Instead of curly braces I have no this quotation signs "". One quotation sign before the value then the second quotation sign that occupies a matrix element for itself and so one. $\endgroup$
    – Torsten
    Jul 28, 2015 at 14:28
  • $\begingroup$ I think the problem is that your data file (thanks for providing it) has spaces after the numbers on each line. The simplest fix seems to be to use First/@Import[..., "Table"] instead of Import[..., "List"]. $\endgroup$ Jul 28, 2015 at 14:34
  • $\begingroup$ Both your suggestions (First/@Import[..., "Table"] and Import[..., "List"]) yield the same result for me. Both with the quotation signs. $\endgroup$
    – Torsten
    Jul 28, 2015 at 14:37
  • $\begingroup$ Using your data file, Export["test.dat", Partition[First /@ Import["rawdata.txt", "Table"], 508], "Table"]; gives me a TSV file with just the numbers and no quotation marks. Could you try Import["rawdata.txt", "Table"] // First // InputForm and tell me what the output is? $\endgroup$ Jul 28, 2015 at 14:44
  • $\begingroup$ If you don't need Mathematica to interpret the input as numbers, just to reformat the contents of the file, you could instead use Export["test.dat", StringJoin @@@ Partition[Import["rawdata.txt", "Lines"], 508], "Lines"];. The format specifier "Lines" instructs Mathematica to treat the contents of the file as strings, and StringJoin combines the columns of the resulting array. $\endgroup$ Jul 28, 2015 at 14:50

Try the following:

(* Import your data as tab-separated values; extra "" will be included *)
Import["c:\\Users\\Marco\\Desktop\\Sandbox\\rawdata.txt", "TSV"];

(* remove the "" caused by the extra tabs in your data file *)
DeleteCases[%, ""];

(* Reshape your data into an array with your required dimensions *)
ArrayReshape[%, {308, 508}];

(* Export the reshaped array to a *.DAT files *)
Export["data.dat", %]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.