1

In the context of automatic report generation, I am trying to use a For loop to Print out a number of Manipulate expressions.

I have made a simple example to illustrate the essence of the problem:

For[i = 1, i <= 3, i++,
  Print[Manipulate[i, {t, 0, 1}]]
  ];

After evaluating this, the following is displayed:

Notebook output

However, I wish the value of a variable, in this case the momentary value of i, to persist in Manipulate. In this case, this would result in 1, 2, and 3 being displayed instead of 4, 4, and 4.

So far, I have tried using Initialization, Module, ReplaceAll, and Unique for this purpose, and surprisingly, nothing worked.

For example, this code yields the same result as the one above:

For[i = 1, i <= 3, i++,
  Print[Manipulate[s, {t, 0, 1}, Initialization :> (s = i)]]
  ];
Jake
  • 155
  • 5
  • I can not understand what you are looking for. Since your Manipulate's control t is in no way coupled to expression it displays in its content pane, there seems no reason at all to be using Manipulate. Can you come up with a better example where the Manipulate expression is actually relevant? – m_goldberg Feb 26 '16 at 00:54
  • Sure, for example with s+t instead of s as the first argument in Manipulate in the second code (but that would distract from the fact that the value of s does not persist in the sense I have described). I tried to make the example as simple as possible; that's one of the debugging strategies that I was taught and rely on. – Jake Feb 26 '16 at 01:17
  • Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Feb 26 '16 at 02:15
  • Hi, Jake, thanks for taking care about the minimal example. Manipulate has a HoldAll attribute so this answers apply: Using pure functions in Table. Does this help? – Kuba Feb 26 '16 at 06:48
  • @Kuba Yes, thanks. :-) – Jake Feb 26 '16 at 16:55

2 Answers2

0

Try DynamicModule.

DynamicModule[{i = #}, Manipulate[i, {t, 0, 1}]] & /@ Range[3] // Column

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143
0

You have a simple scoping problem. Try

For[i = 1, i <= 3, i++, 
  With[{i = i}, Print[Manipulate[i, {t, 0, 1}]]]];

figure

The funny-looking first argument of the With expression, injects the current value of i into the Manipulate expression.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257