I have problems getting Manipulate to work with code assigned to variables that should be evaluated inside the Manipulate statement. Here is how it goes …
test1={a,b,c};
Manipulate[test1,{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
So {a, b, c} are not updated. Ok, whatever, let’s enforce the evaluation of test1
Manipulate[Evaluate[test1],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]

Now it works. But if I want to plot the list of manipulated elements, like this
Manipulate[ListPlot[Evaluate[test1]],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
Manipulate[Evaluate[ListPlot[test1]],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
I end up with

in both chases.
I am aware of ‘Evaluate Expressions inside Dynamic or Manipulate’ in Mathematica’s documentation, but I am pretty sure that it does not provide a solution to my problem.
So the problem is that
test1is defined in terms of global variableGlobal`a,but the
adefined in the manipulate is created by aDynamicModuleand is thus local. This is what acl showed with hisHold[a]example.Maybe the easiest way to fix this is to use
Withto inserttest1into the manipulate:This way the
Manipulatenever actually seestest1, all it sees is{a,b,c}which it then goes on to correctly localize. Although, this will run into problems ifa,b,chave been given a value before theManipulateis run – thus theClear[a,b,c]command.I think that the best practice is to make all local variables completely explicit in the manipulate. So you should do something like
This avoids problems with the global vs local variables that you were having. It also makes it easier for you when you have to come back and read your own code again.
Edit to answer the question in the comments “I really would like to understand why Evaluate does not work with the somewhat nested ListPlot?”. IANLS (I am not Leonid Shifrin) and so I don’t have a perfect Mathematica (non)standard evaluation sequence running in my brain, but I’ll try to explain what’s going on.
Ok, so unlike
Plot,ListPlotdoes not need to localize any variables, so it does not have theAttributeHoldAll.Let’s define something similar to your example:
The final example you gave is like
By looking at the
Trace, you see that this first evaluates the first argument which isListPlot[test] ~> ListPlot[{a,a+1}]and since
ais not yet localized, it produces an empty list plot. To see this, simply runto get the empty graphics object
As the symbolic values
ahave been thrown out, they can not get localized by theManipulateand so not much else happens.This could be fixed by still evaluating the first argument, but not calling
ListPlotuntil afterManipulatehas localized the variables. For example, both of the following workThe fact that
ListPlotthrows away non-numeric values without even the slightest complaint, is probably a feature, but can lead to some annoyingly hard to track bugs (like the one this question pertains to). Maybe a more consistent (but less useful?) behaviour would be to return an unevaluatedListPlotif the plot values are non-numeric… Or to at least issue a warning that some non-numeric points have been discarded.The penultimate example you gave is (more?) interesting, it looks like
Now since
Manipulatehas the attributeHoldAll, the first thing it does is wrap the arguments inHold, so if you look at theTrace, you’ll seeHold[ListPlot[Evaluate[test]]]being carried around. TheEvaluateis not seen, since as described in the Possible Issues section, “Evaluate works only on the first level, directly inside a held function”. This means thattestis not evaluated until after the variables have been localized and so they are taken to be the globalaand not the local (DynamicModule)a.It’s worth thinking about how the following variations work