1
$\begingroup$

I have a simple question.

data1 = {{1, 2}, {2, 3}};
data2 = {{1, 2}, {3, 4}};

Manipulate[ListPlot[chooseData],
 {chooseData, {{data1, data2}}, ControlType -> PopupMenu}
 ]

How to perform this task? Choose to plot data1, or data2 in the Manipulate.

$\endgroup$
  • 1
    $\begingroup$ You have one level of braces too many in the selection list. I addition to that, I'd suggest providing text labels with the names of the data sets for convenience, and pre-selecting an initial value for chooseData. For instance: Manipulate[ListPlot[chooseData], {{chooseData, data1}, {data1 -> "data1", data2 -> "data2"}, ControlType -> PopupMenu} ]. $\endgroup$ – MarcoB Dec 5 '19 at 16:15
5
$\begingroup$

You need to remove one level of braces:

data1 = {{1, 2}, {2, 3}};
data2 = {{1, 2}, {3, 4}};

Manipulate[
  ListPlot[chooseData], 
  {chooseData, {data1, data2}, ControlType -> PopupMenu}
]

Or a bit more sophisticated:

data1 = {{1, 2}, {2, 3}};
data2 = {{1, 2}, {3, 4}};
names = {"data1", "data2"};
ass = Association[#[[1]] -> #[[2]] & /@ 
   Transpose[{names, {data1, data2}}]]

Manipulate[
 ListPlot[ass[chooseData]], {chooseData, names, 
  ControlType -> PopupMenu}]

or also:

data1 = {{1, 2}, {2, 3}};
data2 = {{1, 2}, {3, 4}};

Manipulate[
 ListPlot[ReleaseHold@chooseData], {chooseData, {HoldForm@data1, 
   HoldForm@data2}, ControlType -> PopupMenu}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you. Do you know how to use HoldFirst, or something like that. Because in the list, I want to show the name of the variable, like data1, or data2. Your approach works, but in the PopupMenu, it shows the value of "data1" and "data2" $\endgroup$ – Nam Nguyen Dec 5 '19 at 16:39
  • 2
    $\begingroup$ see the second example (and the third one) :) $\endgroup$ – Fraccalo Dec 5 '19 at 16:41
  • $\begingroup$ wonderful! ReleaseHold! and Association! $\endgroup$ – Nam Nguyen Dec 5 '19 at 16:54

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