# 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 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.

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.

• 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. Jul 28 '15 at 14:12
• Thank you for that suggestion, dionys. And here is the file: s000.tinyupload.com/index.php?file_id=46674322808024643081 Jul 28 '15 at 14:24
• 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. Jul 28 '15 at 14:54

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"]

• 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. Jul 28 '15 at 14:28
• 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"]. Jul 28 '15 at 14:34
• Both your suggestions (First/@Import[..., "Table"] and Import[..., "List"]) yield the same result for me. Both with the quotation signs. Jul 28 '15 at 14:37
• 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? Jul 28 '15 at 14:44
• 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. Jul 28 '15 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"];
Flatten[%];

(* 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", %]