I have an array named graphs
which was calculated during a longer program. Actually, this is an array of Graphics
like frames of a video, but here I just use a simple array with numbers.
I want to show the array elements with Manipulate
and possibly run them like a film.
Case 1: In the first naive approach, I just call Manipulate
with the array and a StepManipulator
-control for the frame number.
The two symbols graphs
within Manipulate
are unevaluated or respectively unknown and highlighted red with a warning "'graphs' in ... occurs where it is probably not going to be evaluated before going out of scope".
Obviously, the graphs
within the Manipulate
is not the same as outside.
Clear[graphs];
Block[{graphs = {1, 20, 30, 40}},
Print[Manipulate[
graphs[[i]]
, {{i, 1, "Step"}, 1, Length[graphs], 1, Appearance -> "Open"}
, LabelStyle -> Directive[Black, Bold, 11],
ControlPlacement -> Bottom
]];
];
graphs
graphs
Case 2: In the second approach, I add an option SaveDefinitions -> True
.
Highlighting and warning remain, but now graphs
takes on its proper value. And when it is changed at the beginning of the Block
, this is reflected within Manipulate
after the block is evaluated.
But graphs
changes as well within the Manipulate
in Case 1 above! Even when I reevaluate Case1 it does not get back its proper Case 1 value. Obviously, the SaveDefinitions
has turned graphs
into a global variable and the definition within Block
is ignored.
(* Case 2 *)
Block[{graphs = {2, 20, 30, 40}},
Print[Manipulate[
graphs[[i]]
, {{i, 1, "Step"}, 1, Length[graphs], 1, Appearance -> "Open"}
, LabelStyle -> Directive[Black, Bold, 11],
ControlPlacement -> Bottom
, SaveDefinitions -> True
]];
];
graphs
{2, 20, 30, 40}
The documentation says "Manipulate is a scoping construct that implements lexical scoping" which might explain some of that unexpected behavior. But why does Case 2 work then? How does the outer graphs
-value from Block
get into Manipulate
?
Anyway, I do not like that SaveDefinitions
-option since it is needless and may lead to a huge notebook on disk.
Case x: I tried some other things with accessing graphs
trough a trivial function defined inside or outside Manipulate
. But this doesn't change anything.
Case 10: What works is to use an intermediate global array globGraphs
fed from graphs.
(* Case 10 *)
Clear[globGraphs];
Block[{graphs = {10, 20, 30, 40}},
globGraphs = graphs;
Print[Manipulate[
globGraphs[[i]]
, {{i, 1, "Step"}, 1, Length[globGraphs], 1, Appearance -> "Open"}
, LabelStyle -> Directive[Black, Bold, 11],
ControlPlacement -> Bottom
]];
];
{graphs, globGraphs}
{{2, 20, 30, 40}, {10, 20, 30, 40}}
Now I have no highlighting and warnings anymore and everything works as expected. But that solution looks pretty strange.
Does anybody have a better solution?
Attachments: