Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain integer values in dynamically linked sliders?

I want to use a pair of sliders to set integer values for two variables nLo and nHi, each of which can individually range from 1 to 100, but subject to the restriction that nHi >= nLo. So I set up a slider for each variable that has a range that depends dynamically on the other variable:

nLo = 1; nHi = 100;
Column[
 {
   Labeled[Slider[Dynamic[nLo], {1, Dynamic[nHi], 1}, 
           Appearance -> "Labeled"], "nLo", Left
   ],
   Labeled[Slider[Dynamic[nHi], {Dynamic[nLo], 100, 1}, 
           Appearance -> "Labeled"], "nHi", Left
   ],
   Dynamic[{nLo, nHi}]
 }
]

The problem is that as soon as I adjust nHi, its value becomes real (displays with a decimal point) rather than integer. I presume that this is because the slider for nHi can't tell that its first range argument Dynamic[nLo] is actually an integer and so it defaults to real values instead. Any suggestions as to how to force nHi to remain integer valued? (Linux Mathematica v8.0.1)

like image 870
renormalize Avatar asked Aug 14 '11 15:08

renormalize


2 Answers

Alternatively you could do something like

nLo = 1; nHi = 100;
Column[{Labeled[
   Slider[Dynamic[nLo], {1, Dynamic[nHi], 1}, 
    Appearance -> "Labeled"], "nLo", Left], 
  Labeled[Slider[
    Dynamic[nHi, (nHi = Round[#]) &], {Dynamic[nLo], 100, 1}, 
    Appearance -> "Labeled"], "nHi", Left], 
  {Dynamic[nLo], Dynamic[nHi]}}]
like image 57
Heike Avatar answered Jan 02 '23 23:01

Heike


Either I fail to understand the requirements of the solution, or this code may only function correctly in Mathematica 7.


Interesting problem. This appears to work:

nLo = 1; nHi = 100;
Column[{Labeled[
   Slider[Dynamic[nLo], {1, Dynamic[nHi], 1}, 
    Appearance -> "Labeled"], "nLo", Left], 
  Labeled[Slider[
    Dynamic[nHi], {Dynamic[Unevaluated@Round@nLo], 100, 1}, 
    Appearance -> "Labeled"], "nHi", Left], 
  Dynamic[{nLo, nHi}]}]
like image 26
Mr.Wizard Avatar answered Jan 02 '23 22:01

Mr.Wizard