Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}*)
share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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
share|improve this answer
    
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. –  Name Apr 4 '12 at 11:23
    
Yes you're right. I worked out what is going on, let me change my answer –  acl Apr 4 '12 at 11:29
    
@Name new answer in place –  acl Apr 4 '12 at 11:38
    
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!). –  Name Apr 4 '12 at 11:46
    
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... –  acl Apr 4 '12 at 14:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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