I am trying to write a program which will generate a list of objects and then exports it in a separate file and clears its memory. As an example I tried the following

Do[{Subscript[l, p] = Table[Subscript[l, p, i], {i, 10}];
  Export["\!\(\*SubscriptBox[\"l\", \"p\"]\).mnaz", 
   Compress[Subscript[l, p]], "String"]; Clear[Subscript[l, p]]}, {p, 
  1, 3}]

Of course, the above is just a sample (and my actual list(s) are more bulky (actually they are list of matrices)), but I hope it explains the spirit. I wanted to export each list separately so that I can get separate files like l_1.mnaz, l_2.mnaz etc., which I can import again by using the same type of code.

Motivation: I need to actually generate a huge list of data. However whatever I do, my system goes out of memory after 5000 loops, which itself takes a lot of time. One reason is that, I need to modify the list at each loop. It seems 1000 loops takes a reasonable time. Hence I am trying to break the loop into smaller chunks, export the data, and clear the memory for the next loop. Clear[Subscript[l, p]] is used for that purpose.

  • $\begingroup$ Subscripts don't work in any filename system I know of, so I think that will cause problems. $\endgroup$
    – Verbeia
    Commented Jul 29, 2012 at 9:11
  • $\begingroup$ @Verbeia even if they did "\!\(\*SubscriptBox[\"l\", \"p\"]\).mnaz" would not work. One could instead use: ToString[Subscript[l, p], TeXForm] <> ".mnaz" or if the file system supports it something besides TeXForm. $\endgroup$
    – Mr.Wizard
    Commented Jul 29, 2012 at 9:25

1 Answer 1


Try this:

 l[p] = Table[Subscript[l, p, i], {i, 10}];
 Export["l_" <> ToString[p] <> ".mnaz", Compress@ l[p], "String"];
 l[p] =.
 , {p, 1, 3}

I replaced Subscript[l, p] with l[p]. Frankly I don't see a reason to not simply use temp = Table[ . . . but I am trying to keep to the spirit of your example.

  • $\begingroup$ Beat me to it by seconds! $\endgroup$
    – Verbeia
    Commented Jul 29, 2012 at 9:12
  • $\begingroup$ @Verbeia I'm trying to defend my rank against the Hypno-Toad onslaught. :o) $\endgroup$
    – Mr.Wizard
    Commented Jul 29, 2012 at 9:16
  • $\begingroup$ Hey! I need two votes for keeping Mr awake all night $\endgroup$ Commented Jul 29, 2012 at 9:40
  • $\begingroup$ @belisarius What's this? Are you colluding with the green one? ;-p $\endgroup$
    – Mr.Wizard
    Commented Jul 29, 2012 at 9:56
  • $\begingroup$ @Mr.Wizard that is just the spirit ;-) $\endgroup$
    – Yves Klett
    Commented Jul 29, 2012 at 9:58

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.