MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Inputting Dynamic Variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103066] Re: [mg103053] Inputting Dynamic Variables
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sun, 6 Sep 2009 07:38:56 -0400 (EDT)
  • References: <200909050938.FAA16371@smc.vnet.net>

Hi Hugh,

I would suggest to change a format of your pp[] somewhat:
make it a list of delayed rules, like this:

pp[] = {"coff a " :> a, "coff b " :> b, "coff c " :> c};

In this way, your symbols are naturally held, without explicit Hold.
I refactored your code to the following then:

---------------------------------------------------------------------------------

ClearAll[a, b, c, pp, inputPanel, setValues, ButtonReSet, F, ReSet];

(*Generic code*)

inputPanel[namesSymbols : {(_String :> _Symbol) ..}] :=
  With[{panel =
     namesSymbols /. (x_ :> y_) :> Row[{x, InputField[Dynamic[y]]}]},
   Dynamic[TableForm[panel]]];

setValues[nameRules : {(_String :> _Symbol) ..},
   valRules : {(_String :> _) ..}] :=
  nameRules /. valRules /. (x_?NumericQ :> y_) :> (y = x);

(*More specific code*)

ButtonReSet[] := Button["New values", setValues[pp[], vals]];

F[] := DynamicModule[{}, Dynamic[Plot[a x^2 + b x + c, {x, -1, 1}]]]

ReSet[] := inputPanel[pp[]]


(*Your initialization code*)

ClearAll[a, b, c];
iv = {a, b, c};
pp[] = {"coff a " :> a, "coff b " :> b, "coff c " :> c};
vals = {"coff a " :> 2, "coff b " :> 3, "coff c " :> 4};
Dynamic[#] & /@ iv


(*And your results,as before*)

ReSet[]

F[]

ButtonReSet[]

-------------------------------------------------------------------------------------

The setValues function does not use ClearAll, but rather assigns values to
held symbols. Also, in this way, you  only need to provide those new values
that did change, rather than provide a list of rules for all values every
time.
Assignments are also performed only for those symbols whose new value is
provided in the value rules - this is ensured by the NumericQ predicate.

Hope this helps.


Regards,
Leonid




On Sat, Sep 5, 2009 at 2:38 AM, Hugh Goyder <h.g.d.goyder at cranfield.ac.uk>wrote:

> I am trying to organize a set of dynamic variables that can be
> modified either by using an input field or by updating the variables
> from a file. When updating from a file I use a button.  I have a large
> number of variables although in the toy example below I just give 3.
> The modules will end up in a private context.  The idea below is that
> the function pp[] contains a list of all the variables in a held form.
> The list named vals can be obtained by reading from a file with new
> values updated from new files.
>
> Below I give an example that works. However, there must be a better
> way... Having a ClearAll in a button seems a poor method for getting
> back to symbols rather than values.  Any suggestion for a better
> approach.?
>
>
>
> ReSet[] := DynamicModule[{},
>  Dynamic[TableForm[
>    Table[InputLine[pp[][[i]]], {i, Length[iv]}]
>    ]]
>  ]
>
> InputLine[{p_, q_}] := DynamicModule[{t},
>  ReleaseHold[ Row[{p, InputField[Dynamic[t]]}] /. t :> q]
>  ]
>
> ButtonReSet[] := Button["New values",
>  Module[{aa, bb},
>   ClearAll[a, b, c];
>   aa = ReleaseHold[pp[][[All, 2]]];
>   bb = pp[][[All, 1]] /. vals;
>   Evaluate[aa] = bb;]]
>
> F[] := DynamicModule[{},
>  Dynamic[Plot[a x^2 + b x + c, {x, -1, 1}]]
>  ]
>
> ClearAll[pp, a, b, c];
> iv = {a, b, c};
> pp[] := {{"coff a ", Hold[a]}, {"coff b ", Hold[b]}, {"coff c ", Hold
> [c]}};
> vals = {"coff a " :> 2, "coff b " :> 3, "coff c " :> 4};
> Dynamic[#] & /@ iv
>
> ReSet[]
>
> F[]
>
> ButtonReSet[]
>
>


  • Prev by Date: Re: Manipulate: How to correctly adjust one control parameters based on current setting of another control?
  • Next by Date: Re: Manipulate: How to correctly adjust one control
  • Previous by thread: Inputting Dynamic Variables
  • Next by thread: Re: Inputting Dynamic Variables