How to use an indexed object inside Manipulate so that its state is preserved as another dynamics?

I really have a big problem in my hands.

I spend a week rewriting the Manipulate demo to use indexed objects (use them as a structure emulation)

However, when I started, I did not know how to define them as a None control type (so that the state was maintained between each Manipulate update), so I moved them to the "Initialization" section now so that I can test the idea.

Everything works well, except for one problem:

In the Initialization section, they become GLOBAL. This means that when creating a demo and creating a snapshot of the Manipulate to be taken, then 2 Manipulations will now interact with each other in an undesirable way. They share these global variables (indexed objects). This means that if I change something in one Manipulate, another manipulator is affected.

The WRI Demo Editor does not recommend using global variables inside the Manipulate section.

I will explain the problem with simple examples and I hope that the expert can know about it. I currently have something like this that works:

 Manipulate[ p@x = 0; (*I'd like to use here *) p@y = 99; (*etc...*) foo, {{foo, 0, "foo"}, 0, 10, 1}, Initialization :> { p[x] = 99; (*my data live here *) p[y] = 0; (*my data live here *) } ] 

But in the above, p [x] and p [y] are global. I tried managing Nothing, but this does not work:

  Manipulate[ p[x] = 0; foo, {{foo, 0, "foo"}, 0, 10, 1}, {{p[x], 99}, None} (* syntax error, what is the correct syntax? *) ] 

I cannot put a module around Manipulate to use it to save state. Is not allowed.

So, I need a way for these indexed objects to maintain state between each Manipulate update, like a control variable, but not global.

But the problem is I do not know how to do this. The only way I knew this was to use the Control No. trick

I know that Manipulate is basically a DynamicModule[] . This is why its own variables (control variables) keep state. I need these indexed objects to look like them. Can I use DynamicModule [] inside Manipulate to do this or is there a simple solution for this?

Btw, I found that I can do the following

 Manipulate[ z, {{foo, 0, "foo"}, 0, 10, 1}, {{z, p[x]}, None} ] 

enter image description here

But I'm not sure what to do with the above. I need to use p @x, not z.

The strange thing is, you can define an array of indexed objects, but not one?

 Manipulate[ z, {{foo, 0, "foo"}, 0, 10, 1}, {{z, Table[p[i], {i, 5}]}, None} ] 

enter image description here

Thanks for any tips.

Update:

I cannot answer Mike below to work as I needed. For example, suppose I want p [x] to initialize to 0, and then in every Manipulate update I want to add it to it. How to do it? This is what I tried:

 Manipulate[ DynamicModule[{p, x}, (*not working *) p@x = p@x + 1; p@x , Initialization :> { p@x = 0; } ], {{n, 0, "n"}, 0, 10, 1}, TrackedSymbols :> n ] 

Will continue to try ...

Update 2:30 AM

The following is a clearer example of the problem in case the above is not clear.

 Manipulate[ p@x = p@x + 1;(*I'd like to use here*) n; Row[{" p@x =", p@x }], Button["click to update p@x ", n++], {{n, 0}, None}, TrackedSymbols :> {n}, Initialization :> { p@x = 0; } ] 

enter image description here

In this example, the indexed object p [x] is a global variable, so its state is preserved. I need to do the same, but without p [x] defined as global, but move it as part of the Manipulation so that it becomes localized, but also retains its state.

The problem again is that the control None syntax does not allow me to type

 {{ p@x ,0},None} 

I hope this example simplifies the situation.

+4
source share
1 answer

With the caveat that I mainly use DynamicModule, rarely Manipulate, so I am not familiar with how you are trying to serve it, I think this might work:

 Manipulate[ DynamicModule[{p, x, y}, p@x = 0; p@y = 99; p[y]*foo (* or Dynamic[p[x]*foo] *)], {{foo, 0, "foo"}, 0, 10, 1}] 

If it does not work as you need, you may need to provide additional information or wait for Manipulate to respond.

Edit

Just added alternative Dynamic. So in your real code, if, for example, p or x or y should be updated, then you need to use Dynamic. (In the example above, suppose x is dynamic)

FURTHER IMAGE

Your last edit implies that you want to change p @x when the value of n changes, for example. when the engine moves.

 Manipulate[ DynamicModule[{p, x, tmp}, p@x = 0; {Dynamic[ p@x += 1; n, TrackedSymbols :> {n}], Dynamic[ p@x ]}], {{n, 0, "n"}, 0, 10, 1}] 

This is for me today. Maybe someone else can offer some suggestions.

+2
source

Source: https://habr.com/ru/post/1386809/


All Articles