3
$\begingroup$

I would like to know a method to share variables between Manipulate environments. I understand that the variables used in controllers are a kind of local ones and cannot be shared globally. So I 'copied' them (x[i]) to global variables (x2[i]) in this example code. However, it didn't work because the values of the local variables are not transmitted while symbols were soundly reflected. My question is how we can retrieve values of local variables in the controllers from outside.

    Manipulate @@ 
 Join[{{Table[x2[i] = x[i], {i, 1, 5}], Table[x2[i], {i, 1, 5}]}},
  Table[{x[i], 1, 10, 1}, {i, 1, 5}]]

enter image description here

and

Manipulate[x2[i], {i, 1, 5, 1}]

enter image description here

x[1] is supposed to be 6.

$\endgroup$
3
  • 1
    $\begingroup$ Is there something missing from your code? When I run this x2[i] is not set and shows up as x2[1],x2[2],.... $\endgroup$
    – N.J.Evans
    May 1 '20 at 13:18
  • 1
    $\begingroup$ If you're still looking, Szabolcs' answer here seems to address something similar. You'll probably want to get rid of the Block which is used to explicitly localize the variable. $\endgroup$
    – N.J.Evans
    May 5 '20 at 15:01
  • 1
    $\begingroup$ Evans, Thank you for you comments. Sorry, I fixed my miscoded code. It seems that Manipulate has some kind of limitation, and Szabolcs' answer may provide me a nice clue. $\endgroup$ May 7 '20 at 10:55
3
$\begingroup$

You can share variables without copying. Manipulate does not try to keep symbols local (edit: the control symbols are local (i,j), but you can assign them like I did here to get them globally).

 Manipulate[
 {x[1] = i, x[2] = j},
 {i, 1, 5},
 {j, 1, 5}
 ]

Manipulate[
 x[k], {k, 1, 2, 1}
 ] 

I believe your problem is a more subtle one having to do with the way things are evaluated when you create tables of variables and then apply Manipulate to them. I have not figured out how to make this work yet. If you only have 5 symbols to assign, this solution could work in the meantime.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you. I understand your point. In the meantime, I wanted to make an arbitrary number of controllers with indexed variables like x[i]. Probably my problem can be reduced to how I can use indexed variables in a controller of Manipulate. It seems to work, but I cannot retrieve the indexed variable from outside like your code. I hope I can have a solution for that... $\endgroup$ May 1 '20 at 14:53

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.