Message Boards Message Boards

[✓] Choose dates dynamically with a Slider?

GROUPS:

I would like to select a date from a list of dates using a slider, and show the chosen date right underneath the slider in the form of a date string.. Take for instance the following simple list:

list={{2000, 1, 3}, {2000, 1, 4}, {2000, 1, 5}, {2000, 1, 6}, {2000, 1, 7}, {2000, 1, 10}, {2000, 1, 11}, {2000, 1, 12}, {2000, 1, 13}, {2000, 1, 14}}

So I could for instance choose with the slider the third element of this list, which is {2000, 1,5}, and then convert this with the command DateString[DatObject[{2000, 1, 7}]] to the data string "Wed 5 Jan 2000". Now I want this date string shown underneath the slider instead of the usual number, which in this case would be 3.

The slider would be for instance:

Slider[Dynamic[m],{1,10,1}

The problem is now that Dynamic[m] is not accepted by Part. With the command

list[[Dynamic[m]]]

one gets an error message that the result from the slider cannot be used as a part specification. How do I solve this problem? Thanks in advance for your help.

POSTED BY: Laurens Wachters
Answer
21 days ago

Laurens,

Put the Dynamic on the outside of the list, not in the part specification:

Dynamic[list[[m]]]

Putting it together you get:

Column[{Slider[Dynamic[m], {1, 10, 1}], 
  Dynamic[DateString[DateObject[list[[m]]]]]}]

Regards,

Neil

POSTED BY: Neil Singer
Answer
21 days ago

Dynamic then should be an outer wrapper not the inner:

list={
    {2000,1,3},{2000,1,4},{2000,1,5},{2000,1,6},{2000,1,7},
    {2000,1,10},{2000,1,11},{2000,1,12},{2000,1,13},{2000,1,14}};

Method 1 - indexed variable

Column[{
    Slider[Dynamic[m],{1,10,1}],
    Dynamic[DateString[DateObject[list[[m]]]]]}]

enter image description here

Method 2 - list of variables

Column[{
    Slider[Dynamic[m],{DateString[DateObject[#]]&/@list}],
    Dynamic[m]}]

Method 3 - localized variable

Manipulate[DateString[DateObject[m]], {m, list, Slider}]
POSTED BY: Vitaliy Kaurov
Answer
21 days ago

Thanks a lot for your advice. I have learnt a bit on Dynamic by now. But I am now stuck with Dynamic applied on FinancialData. I have made the following small program that should become a part of a cdf for analyzing the course of stock prices in which the user would be able to switch from stock to stock

p="ge";
Column[{DynamicModule[InputField[Dynamic[p]], 
Dynamic[ListLinePlot[FinancialData[p,{2017,10,1}][[All,2]]]]}]

This program does work, but after its evaluation my PC becomes awfully slow. Why is this, and would there be a remedy for it? I have a PC with an Intel i5 processor and 8 MB memory. Mathematica and its kernel don't seem to use very much of them, according to the windows 10 task manager. Another question is why in the result the input field and the plot are shown in a row and not in a column.

POSTED BY: Laurens Wachters
Answer
11 days ago

Laurens,

Your posted code did not have matching delimiters but I got the same behavior (once fixing it) because the FinancialData call is time consuming.

I fixed it one way (the easy way --- but more limiting) with this code by using a Manipulate.

Manipulate[
 ListLinePlot[FinancialData[p, {2017, 10, 1}][[All, 2]]], {p, "ge"}, 
 ControlType -> InputField]

You can use Dynamic directly but you must handle more details this way:

DynamicModule[{p, plotfun, plot}, p = "ge"; 
 plotfun[a_, b_] := 
  plot = ListLinePlot[FinancialData[a, {2017, 10, 1}][[All, 2]]]; 
 plotfun[p, p]; 
 Column[{InputField[Dynamic[p, {Automatic, plotfun}], String], 
   Dynamic[plot]}]]

The way this works is to make the variables p, plotfun (the function for plotting), and the plot itself local . This is so we do not mess with global variables (although you can make them global if you want). I define a function to make the plot called "plotfun" so I can call it two times -- once at initialization so we start with a default plot and once in operation. Note that I used String as the second argument to InputField so we do not need to deal with the quotation marks. I use the optional arguments to Dynamic {fstart,f,fend}. Read about these but I defined them to be {Automatic, plotfun} -- during editing the inputfield MMA automatically updates the variable "p" but at the end (when you hit tab or return) it runs the function "plotfun" (which updates our plot --- only once!!! so we do not bring the machine to its knees repeatedly getting financial data.

Regards

Neil

POSTED BY: Neil Singer
Answer
11 days ago

Group Abstract Group Abstract