3
$\begingroup$

I have the following multiplication Manipulate:

Manipulate[
   x*y,
   {x, 1, 10},
   {y, 5, 15}
              ];

When you run this Manipulate, the X and Y sliders should not be visible to the user. But, The question: What is your X value between 1 and 10? should pop up with an empty box for entering a number for X between 1 and 10. After entering an X value, then the second question: What is your Y value between 5 and 15? should pop up with another empty box for entering a Y value. After answering both questions, the answer should be visible in Manipulate pane. Every time you run this Manipulate, the same steps should be evoked.

Can we do that in Mathematica?

$\endgroup$

2 Answers 2

3
$\begingroup$

Edit: For the revised question, as described it is not clear why you want a Manipulate as opposed to using Input.

Clear["Global`*"]

Using Slider

Panel[
 x = 0;
 y = 0;
 result = {};
 While[Not[IntegerQ[x] && 1 <= x <= 10], 
  (x = Input[
     "What is your X value between 1 and 10?"])];
 While[Not[IntegerQ[y] && 5 <= y <= 15],
  (y = Input[
     "What is your Y value between 5 and 15?"])];
 result = x*y;
 If[result === {}, "",
  Manipulate[
   xx*yy,
   {{xx, x, "x"}, 1, 10, 1, Appearance -> "Labeled"},
   {{yy, y, "y"}, 5, 15, 1, Appearance -> "Labeled"}]], 
 "Product of X and Y"]

Or using SetterBar

Panel[
 x = 0;
 y = 0;
 result = {};
 While[Not[IntegerQ[x] && 1 <= x <= 10], (x = Input[
     "What is your X value between 1 and 10?"])];
 While[Not[IntegerQ[y] && 5 <= y <= 15],
  (y = Input[
     "What is your Y value between 5 and 15?"])];
 result = x*y;
 If[result === {}, "",
  Manipulate[
   xx*yy,
   {{xx, x, "x"}, Range[10], ControlType -> SetterBar},
   {{yy, y, "y"}, Range[5, 15], ControlType -> SetterBar}]],
 "Product of X and Y"]

THIS IS AN EXTENDED COMMENT RATHER THAN AN ANSWER.

Instead of a Slider you should consider using a PopupMenu to choose amongst alternatives.

Define a list of countries

countries = CountryData[#, "Name"] & /@
   CountryData["SouthAmerica"];

Define a list of sectors (shortened here)

sectors = {
   "AGF: Agriculture/hunting/fishery",
   "CO12: Crude oil/mining",
   "MA1: Manufacturing/petroleum refining",
   "MA2: Manufacturing-other"};

Manipulate[
 {country, sector},
 {{country, 1, "Choose a country"}, 
  Thread[Range[Length[countries]] -> countries], ControlType -> PopupMenu},
 {{sector, 1, "Choose a sector for targeting"}, 
  Thread[Range[Length[sectors]] -> sectors], ControlType -> PopupMenu}]

enter image description here

$\endgroup$
5
  • $\begingroup$ It is a very useful comment to improve the appearance of the Manipulate window. $\endgroup$ Jul 25, 2020 at 9:34
  • $\begingroup$ Thanks for this. It works the way I expected. Since I already have various standalone Manipulate codes involving heavy computations, I would like to stick to those instead of revising them with your proposal. It is a kind of path dependency problem. In your edited answer above, would it be possible to see the final product X*Y within the original Manipulate pane showing X and Y sliders for the user to continue choosing new values from the sliders, instead of running the code again? $\endgroup$ Jul 27, 2020 at 13:05
  • $\begingroup$ Thanks for your effort. It is what I was imagining. Referring to your earlier comment above, would it be possible to enter input X and Y values by using a Popup` menu? After the first question, a Popup menu comes and the choice is made. Then, the second question comes with a Popup menu. After both choices are made, the Manipulate becomes visible. Just like you did above, only difference is to use the Popup menu. I hope I am not asking too much. Thank you very much. $\endgroup$ Jul 27, 2020 at 18:16
  • $\begingroup$ In the example using SetterBar change both instances of SetterBar to PopupMenu. However, both questions must be answered in the input fields before the Manipulate will appear. If you only want PopupMenus, then use a straightforward Manipulate as in the original extended comment. $\endgroup$
    – Bob Hanlon
    Jul 27, 2020 at 18:49
  • $\begingroup$ Thanks very much for this nice piece. $\endgroup$ Jul 27, 2020 at 19:35
2
+25
$\begingroup$

Slider is best for adjust Reals. SetterBar is nice to Integer.

Like everything else a lot is a matter of taste. Some like it more individualizable.

Manipulate[
 Which[typeSelected == 1, Row[{"Product of x * y = ", x*y}], 
  typeSelected == 2, Row[{"Product x * y =  ", x*y}], True, 
  "No way ! Please report a bug"], 
 Grid[{{Style["Selection of x and y", 10], SpanFromLeft}, {"Select", 
    TabView[{{1, 
       "x" -> Row[{"x= ", 
          SetterBar[Dynamic[x, {x = #} &], Range[10]]}]}, {2, 
       "y" -> Row[{"y= ", 
          SetterBar[Dynamic[y, {y = #} &], Range[11] + 4], 
          Dynamic[y]}]}}, Dynamic[typeSelected]]}}, Frame -> All, 
  Spacings -> .5, FrameStyle -> Gray], {{x, 1}, None}, {{y, 1}, 
  None}, {{typeSelected, 1}, None}, 
 TrackedSymbols :> {x, y, typeSelected}]

Manipulate with nice layout

Some nice too is:

{Slider2D[Dynamic[y], {{1, 5}, {10, 15}, {1, 1}}, 
  Appearance -> "Labeled"], Dynamic[y[[1]]*y[[2]]]}

Slider2D labeled

A more sophisticated example:

DynamicModule[{pt = {3, 7}}, {LocatorPane[Dynamic[pt], 
   Graphics[Rectangle[], 
    GridLines -> {{1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10}, {5, 6, 7, 8, 9, 
       10, 11, 12, 13, 14}}, Frame -> True, 
    PlotRange -> {{1, 10}, {5, 15}}]], Dynamic[Round@pt], 
  Dynamic[Round@pt[[1]] Round@pt[[2]]]}]

DynamicModule with a Plot background with GridLines

Somehow Wolfram Inc. is working on Manipulate still. Have a look at

ExperimentalExplore[]`

$\endgroup$
1
  • $\begingroup$ Thank you for your variety of codes. I will keep them in my repository for more advanced work. $\endgroup$ Jul 27, 2020 at 19:37

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.