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.

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?

share|improve this question
    
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... –  rasher Jun 10 at 1:52
    
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. –  Kuba Jun 10 at 5:09
    
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}.... –  Kuba Jun 10 at 5:12

1 Answer 1

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

Manipulate[
 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.

share|improve this answer

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.