Message Boards Message Boards

2
|
6991 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Unexpected SetterBar behavior when Manipulate nested in DynamicModule

Posted 11 years ago
Hello everyone! I'm rather new to Mathematica, and although I understand the basics regarding Module, Block, and DynamicModule, I have come across some unexpected behavior when I use Manipulate to change the graph to be plotted when inside a DynamicModule. Below is the code for when I wrap the Manipulate with DynamicModule:

DynamicModule[{x, y},
x[t_] := t^2 + 5;
y[t_] := t^3 + 5;

Manipulate[Plot[fcn[t], {t, -5, 5}],
  {fcn, {x, y}}
  ]
]
And here is the result:


The SetterBar behaves as expected when you click between the square function and the cubic function (with the square function selected above), but the SetterBar looks differently than the following code:
x[t_] := t^2 + 5;
y[t_] := t^3 + 5;

Manipulate[Plot[fcn[t], {t, -5, 5}],
{fcn, {x, y}}
]




I understand that the variable names are different between the two examples because DynamicModule localizes the variables (by adding $$ and numbers after the variable name), but why did the highlighting functionality stop working? It is also a curious thing to note that replacing DynamicModule with either Block or Module will produce the expected behavior for the SetterBar.

One other thing I noticed - Why is the function not offset by 5 like expected?
POSTED BY: Tom McQuilken
5 Replies
The behavior of the dynamic variables was answered.

Your other issue has to do with hown displays graphics by default. If you look carefully at the graph, the graph starts at y = 5. To make sure that the axes go through {0,0}, you have to use the option AxesOrigin.
x[t_] := t^2 + 5;
y[t_] := t^3 + 5;
Manipulate[ Plot[fcn[t], {t, -5, 5}, AxesOrigin -> {0, 0}], {fcn, {x, y}}]
That should do the trick. There are other things to look at, such as PlotRange, that affect how the plot is drawn.
Posted 11 years ago
George, thanks for the information about the plotting. Do you have any input regarding why the highlight behavior for the SetterBar does not work when the Manipulate is wrapped around with a DynamicModule? That is what I would really like to know, and I can't seem to find the answer!
Thanks,
Tom
POSTED BY: Tom McQuilken
DynamicModule and it's ilk make new variables via $ModuleNumber, for instance:
DynamicModule[{var}, Dynamic@var]

The way I would get around your issue with the labels of the SetterBar is by providing labels:
DynamicModule[{x, y}, x[t_] := t^2 + 5;
y[t_] := t^3 + 5;
Manipulate[
  Plot[fcn[t], {t, -5, 5}], {fcn, {x -> "hello", y -> "world"}}]]

Actually, this may not answer your question. But it might be useful for you to know labels can be provided.
POSTED BY: Martin Hadley
Posted 11 years ago
Martin, Thanks for the reply. That is a wonderful thing to point out, and I'm glad you did. You can use a rule in the Manipulate construct to override the default label. However, this does not address the problem I have, which can be summarized as such:

Why does the SetterBar highlighting behavior not work properly when I wrap the Manipulate with a DynamicModule? Some input would be much appreciated!
Thanks,
Tom
POSTED BY: Tom McQuilken
Posted 11 years ago
I haven't looked into this very closely, but I see the following option setting in the Manipulate`ManipulateBoxes subexpression in the cell expression of the output:

"Specifications" :> {{$CellContext`fcn$$, {$CellContext`x$$, $CellContext`y$$}}}

This seems to determine the control labels we see. If $$ is removed from both of the localized x and y symbols, the control labels appear "normally" when you revert the cell expression --- and everything seems to work fine still, with the added bonus that the buttons are highlighted when they are selected.

I think an interesting thing to note here is that fcn$$ "works" fine, i.e. it displays without $$ in the control label. Also, if you remove $$ from fcn$$, the control (understandably) no longer sets the localized fcn symbol.

Sorry I don't have an answer for you, but this is interesting and I'd also like to know why it's happening.
POSTED BY: William Rummler
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract