3
$\begingroup$

The following code displays a plot and tries to display the x=1 value of the same plot in the control area:

DynamicModule[{lbl}, 
 Manipulate[lbl = ToString[Sin[x (1 + a x)] /. x -> 1]; 
  Labeled[Plot[Sin[x (1 + a x)], {x, 0, 6}], lbl], 
  Column[{Style[lbl, Bold, Large], Control[{a, 0, 2}]}]]]

enter image description here

Is there a way to scope the lbl variable so that it is updated properly in the display?

$\endgroup$
4
  • 3
    $\begingroup$ you can simply wrap the Style with Dynamic. Note that you will always get 0 anyway, because that is the correct value following the substitution x -> 0 $\endgroup$
    – glS
    Oct 18, 2016 at 10:28
  • $\begingroup$ if $x=0$ you have $\sin(0\times( 1+a \times 0)) = \sin(0) =0$. Anyway, doesn't Dynamic solve your problem? $\endgroup$
    – glS
    Oct 18, 2016 at 13:15
  • $\begingroup$ @dionys Maybe there's a typo? I get 0. under the plot: i.stack.imgur.com/b5Jhw.png -- but it does seem to be a fairly irrelevant side-issue compared to getting lbl to display. $\endgroup$
    – Michael E2
    Oct 18, 2016 at 13:26
  • $\begingroup$ @glS Apologies. Yes, with a fresh kernel the original label would always be zero. I was just trying to throw an example together quickly and didn't notice that Global`x was leaking in. $\endgroup$
    – dionys
    Oct 18, 2016 at 15:27

2 Answers 2

3
$\begingroup$

Here's a variation that has the label above (in the control area) and below (in the plotlabel area). The trick is to have the Dynamic wrap around each of the instances of the lbl variable.

Manipulate[lbl = ToString[Sin[x (1 + a x)] /. x -> 1];
 Labeled[Plot[Sin[x (1 + a x)], {x, 0, 6}], Dynamic[lbl]], 
 Column[{Style[Dynamic[lbl], Bold, Large], Control[{a, 0, 2}]}]]

enter image description here

$\endgroup$
0
0
$\begingroup$

Why do you not like

Manipulate[
 Plot[Sin[x (1 + a x)], {x, 0, 6}, 
  PlotLabel -> Style[Sin[x (1 + a x)], Bold, FontSize -> 18]], {a, 0, 
  2}]
$\endgroup$
4
  • 1
    $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this meta Q&A helpful $\endgroup$
    – Michael E2
    Oct 18, 2016 at 12:17
  • 1
    $\begingroup$ The example is a simplified demonstration. I don't care specifically about this plot. What I would like to do is dynamically update the label (or include more complicated updating) in the controls. $\endgroup$
    – dionys
    Oct 18, 2016 at 12:57
  • $\begingroup$ I must be missing something. The code above updates the label as you move the slider. What more do you seek? $\endgroup$
    – Nicholas G
    Oct 18, 2016 at 14:06
  • $\begingroup$ @NicholasG The key here is "in the control area" ... it entails a different evaluation track because of how this part is executed inside of Manipulate. $\endgroup$
    – dionys
    Oct 18, 2016 at 15:04

Your Answer

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

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