1
$\begingroup$

I want to make a selector that allows me to select a date and, depending on the date, load a list of car makes. After that, I should be able to select a specific car make.

  Manipulate[
    years = 
  Import["http://www.fueleconomy.gov/ws/rest/vehicle/menu/year", 
   "XML"];
    y = Cases[years, XMLElement["menuItem", _, _], Infinity] /. 
    XMLElement[_, _, {val_}] ->  val /. 
   XMLElement["menuItem", _, list_] -> list;
    Do[y[[i]] = DeleteDuplicates[y[[i]]], {i, 1, Length[y], 1}];
    years = y[[All, 1]];

    makes = 
  Import[StringJoin[
    "http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=", 
    ToString@year], "XML"];
    m = Cases[makes, XMLElement["menuItem", _, _], Infinity] /. 
    XMLElement[_, _, {val_}] ->  val /. 
   XMLElement["menuItem", _, list_] -> list;
    Do[m[[i]] = DeleteDuplicates[m[[i]]], {i, 1, Length[m], 1}];
    makes = m[[All, 1]];

    models = 
  Import[URLEncode[
    StringJoin[
     "http://www.fueleconomy.gov/ws/rest/vehicle/menu/model?year=", 
     ToString@year, "&make=", ToString@make]], "XML"];
    mo = Cases[models, XMLElement["menuItem", _, _], Infinity] /. 
    XMLElement[_, _, {val_}] ->  val /. 
   XMLElement["menuItem", _, list_] -> list;
    Do[mo[[i]] = DeleteDuplicates[mo[[i]]], {i, 1, Length[mo], 1}];
    models = mo[[All, 1]];

    options = 
  Import[URLEncode[
    StringJoin[
     "http://www.fueleconomy.gov/ws/rest/vehicle/menu/options?year=", 
     ToString@year, "&make=", ToString@make, "&model=", 
     ToString@model]], "XML"];
 o = Cases[options, XMLElement["menuItem", _, _], Infinity] /. 
    XMLElement[_, _, {val_}] ->  val /. 
   XMLElement["menuItem", _, list_] -> list;
    Do[o[[i]] = DeleteDuplicates[o[[i]]], {i, 1, Length[o], 1}];
    options = o[[All, 1]];

    Row[{options}],
    {year, years},
    {make, makes},
    {model, models},
    {option, options},
    TrackedSymbols :> {make, year, model, option}
 ]

The problem is that, when I change the year, the car makes list should also change; the change shows up in the contents pane, but is not reflected in the control specified by {make, makes}.

$\endgroup$
3
$\begingroup$

I made a few changes to your code: First, I moved all of the Import code to outside of the Manipulate. This is better for interactive performance and prevents the same Import from being done multiple times. I put the makes of all available years in the two dimensional list makes (so any row now contains all the makes for a specific year). I also added a TrackedSymbols option to prevent the Manipulate from continuously executing (what it did in your implementation).

years = Import["http://www.fueleconomy.gov/ws/rest/vehicle/menu/year", "XML"];
y = Cases[years, XMLElement["menuItem", _, _], Infinity] /. 
   XMLElement[_, _, {val_}] :> val /. 
  XMLElement["menuItem", _, list_] :> list;
Do[y[[i]] = DeleteDuplicates[y[[i]]], {i, 1, Length[y], 1}];
years = y[[All, 1]];
makes =
  (
     makes = 
      Import[StringJoin[
        "http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=", 
        ToString@#], "XML"];
     m = Cases[makes, XMLElement["menuItem", _, _], Infinity] /. 
        XMLElement[_, _, {val_}] :> val /. 
       XMLElement["menuItem", _, list_] :> list;
     Do[m[[i]] = DeleteDuplicates[m[[i]]], {i, 1, Length[m], 1}];
     m[[All, 1]]
     ) & /@ years;

Manipulate[
   Grid[{{"Possible choices:  ", makes[[yearNum]]}, {"Chosen:  ",  make}}],
   {{make, "Audi"}, makes[[yearNum]], ControlType -> PopupMenu},
   {{yearNum, 1, "Year"}, Thread[Range[Length@years] -> years]},
   TrackedSymbols :> {make, yearNum}
]

Mathematica graphics

|improve this answer|||||
$\endgroup$
  • $\begingroup$ I tried it but it don't works for me, also i change a bit the code, what i want is change content of popup menu depending the value picked at previous popup $\endgroup$ – Alberto Jul 23 '15 at 10:13
  • $\begingroup$ @alberto See update. $\endgroup$ – Sjoerd C. de Vries Jul 23 '15 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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