12
$\begingroup$

Could anyone explain to me what in the world is going on here and please help me in reading in dummy.dat properly?

dummy = Table[{k, 2*k/m}, {k, 1, 10}, {m, 1, 10}];    
Dimensions[dummy]    
(*{10, 10, 2}*)

Export["dummy.dat", dummy];    
dummyImport = Import["dummy.dat", "Data"];    
Dimensions[dummyImport]    
(*{10, 20}*)

Edit: Just in case anyone exports their precious data as above and needs to read it in properly, the following simple fix worked for me:

dummyReconst = Table[
{
ToExpression[StringJoin[StringCases[dummyImport[[m, k]], RegularExpression["[^{},]"]]]],
ToExpression[StringJoin[StringCases[dummyImport[[m, k + 1]], RegularExpression["[^{},]"]]]]}, 
{m, 1, Dimensions[dummyImport][[1]]
}, {k, 1, Dimensions[dummyImport][[2]], 2}
];

Dimensions[dummyReconst]
(*{10, 10, 2}*)
$\endgroup$
8
$\begingroup$

What is happening is that your data is imported as strings:

dummy = Table[{k, 2*k}, {k, 1, 3}, {m, 1, 3}]
Dimensions[dummy]
Export["~/Desktop/dummy.dat", dummy]; dummyImport = 
 Import["~/Desktop/dummy.dat"]
Dimensions[dummyImport] 
Map[Head, dummy, {-1}]
Map[Head, dummyImport, {-1}]

enter image description here

One way to fix it is to force the save to occur in some particular format:

dummy = Table[{k, 2*k}, {k, 1, 3}, {m, 1, 3}]
Dimensions[dummy]
Export["~/Desktop/dummy.dat", dummy, "MAT"]; dummyImport = 
 Import["~/Desktop/dummy.dat", "MAT"]
Dimensions[dummyImport] 

Map[Head, dummy, {-1}]
Map[Head, dummyImport, {-1}]

enter image description here

Alternatively, DumpSave, which uses an OS-dependent format, may be used to save and reload parts of the environment

DumpSave["~/Desktop/dummy.mx", dummy];
ClearAll[dummy];
dummy
Import["~/Desktop/dummy.mx"]
dummy
$\endgroup$
  • $\begingroup$ Thanks very much for your quick answer. When I do what you suggest, I get this: codeIn[29]:= dummy = Table[{k, 2*k/m}, {k, 1, 10}, {m, 1, 10}]; In[30]:= Dimensions[dummy] Out[30]= {10, 10, 2} In[31]:= Export["~/Desktop/dummy.csv", dummy]; In[32]:= dummyImport = Import["~/Desktop/dummy.csv"]; In[33]:= Dimensions[dummyImport] Out[33]= {10, 10} So still not the right results. $\endgroup$ – Name Apr 4 '12 at 11:23
  • $\begingroup$ Yes you're right. I worked out what is going on, let me change my answer $\endgroup$ – acl Apr 4 '12 at 11:29
  • $\begingroup$ @Name new answer in place $\endgroup$ – acl Apr 4 '12 at 11:38
  • $\begingroup$ acl, thanks so much for this. How did you come up with this "MAT" option? Is this something you just have to know? At any rate, I'll save this answer for future reference ... the actual data I exported and am now trying to import took a couple of hours to compute, so maybe I'll have to try some regular expression stuff. Kind of disappointing how Mathematica fails on simple issues. Anyways, thanks again for your help and cheers to Dresden (Julicher gave a really fantastic talk over here a little while ago!). $\endgroup$ – Name Apr 4 '12 at 11:46
  • $\begingroup$ I was just looking for some way to get mma to realize these were real numbers and not strings. knowing that Export supports MAT types (Matlab format) helped... $\endgroup$ – acl Apr 4 '12 at 14:41
0
$\begingroup$

Export it to a txt file as a "String" and ToExpression@Import["dummy.txt"]

$\endgroup$
0
$\begingroup$

One can also import and export as "*.mx" files, as this will preserve Mathematica's 'lists of lists' formatting. Using the original dummy example, one would have:

dummy = Table[{k, 2*k/m}, {k, 1, 10}, {m, 1, 10}];    
Dimensions[dummy]    
(*{10, 10, 2}*)

Export["dummy.mx", dummy];

dummyImport = Import["dummy.mx"];    
Dimensions[dummyImport]    
(*{10, 20, 2}*)

Also see the discussion in the link below: How do I save/export a list so that it can later be easily imported as a list again?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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