I've been having an issue in a project I am working on for a demonstration that involves a manipulate containing buttons that influence local variables. In essence, the problem is demonstrated by this piece of code:

Manipulate[DynamicModule[{T = 5},

Dynamic[Panel[Row[{T, Spacer[50], Button["T", T = T + 1]}]]]], {x, 
1, 4}]

Before Closing:

enter image description here

After Reopening:

enter image description here

It works fine as far as functionality, but when I try to create a thumbnail and snapshot with the output, the values displayed are not saved if I close and reopen the file (in the case of this program, the display of of T would be reset to 5). How can I get around this?

  • $\begingroup$ I'm not clear what you're after. Is it that you want to close the notebook containing your code (or quit Mathematica for that matter), but have it remember state on reopening? As in you make a pic of the state, associate it with the notebook, and when opened it matches the pic? Then you'll need to save the state and read it on open, via things like Save, Get, Dumpsave, Dumpget, Put, among others, or just write/read from an associated file on close/open... $\endgroup$
    – ciao
    Jun 10 '14 at 1:52
  • $\begingroup$ Change the order of DynamicModule and Manipulate: so DynamicModule[{T=5}, Manipulate[.... Also, remember that everything in Manipulate is effectively wrapped in Dynamic. Another point, the less Dynamic the better so in this case I'd use Dynamic[T], Spacer... without Dynamic on Panel. $\endgroup$
    – Kuba
    Jun 10 '14 at 5:09
  • $\begingroup$ You can use this DynamicModule[{x = 5}, Dynamic[Print@1; x]] to see that after reopening 1 will be printed, Dynamic recreates the content and in your case it is DynamicModule[{T=5}.... $\endgroup$
    – Kuba
    Jun 10 '14 at 5:12

Your example code mixes things up unfortunately. What you need to do is stick with DynamicModule, in which case Manipulate is redundant, or stick with Manipulate in which case DynamicModule is redundant. Personally I don't like Manipulate at all and would recommend DynamicModule.

DynamicModule[{T = 5}, 
 Dynamic[Panel[Row[{T, Spacer[50], Button["T", T = T + 1]}]]]]

If you must use Manipulate then

 Panel[Row[{T, Spacer[50], Button["T", T = T + 1]}]], {x, 1, 4}, {T, 
  5, None}]

In both the above examples the value of T is preserved across sessions. The screen grab is taken after closing Mma and reopening.

enter image description here

For your real world example I would recommend paying close attention to the syntax colouring so you can identify which variables belong to what and will have values preserved.


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.