0
$\begingroup$

Say I have an array of data arrays, assigned to a temporary variable dat, and I'd like to switch between plots of the arrays using Manipulate. For example:

RandomData := Table[RandomReal[], {ii, 1, 3}, {jj, 1, 10}];
dat = RandomData
Manipulate[
 ListPlot[
  dat[[ii]]
  ]
 , {ii, 1, Length@dat, 1}]

Later, I generate another set of data arrays, and I want the same kind of dynamic plot. But I'd like to assign the data to the same temporary variable and reuse the plotting code to avoid needing to rename all the data variables within the Manipulate block (in general, the block could be long and complicated with many calls to dat).

dat = RandomData
Manipulate[
 ListPlot[
  dat[[ii]]
  ]
 , {ii, 1, Length@dat, 1}]

The problem, of course, is that if I reassign a new value to dat, then the earlier Manipulate plot updates with the new value. But I would like both Manipulate outputs to show the value of dat as it was when the Manipulate block was called!

Is there a way to restrict the dynamic updating only to the local variable ii (i.e. to disable updating the variable dat)? Or is there perhaps an entirely different way to go about this, with Module, perhaps?

$\endgroup$
1
  • 2
    $\begingroup$ This is what DynamicModule is for $\endgroup$
    – flinty
    Jan 6, 2022 at 18:34

1 Answer 1

1
$\begingroup$

You can scope the updating of dat. e.g.

DynamicModule[{dat = RandomData}, 
 Manipulate[ListPlot[dat[[ii]]], {ii, 1, Length@dat, 1}]]
$\endgroup$
1
  • $\begingroup$ Ahh, I knew Mathematica somehow had my back! Thank you. $\endgroup$
    – Gilbert
    Jan 6, 2022 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.