Re: Variable amount of Buttons in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg92791] Re: Variable amount of Buttons in Mathematica
- From: "David Park" <djmpark at comcast.net>
- Date: Mon, 13 Oct 2008 06:20:12 -0400 (EDT)
- References: <gcscrf$8l3$1@smc.vnet.net>
The problem is that the second argument of Button is maintained unevaluated.
So n in Print[n] does not get filled in by the iterator. The following trick
will work. The With statement replaces n with the iterator value everywhere
in the Button statement.
array = {1, 2, 3, 4, 5};
Row[Table[
With[{n = n},
Button[array[[n]], Print[n], ImageSize -> {50, 50}]],
{n, 1, Length[array], 1}]]
--
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/
"SDY" <youngsd2 at gmail.com> wrote in message
news:gcscrf$8l3$1 at smc.vnet.net...
> I'm pretty new to this...
>
> I'm trying to write a function that will create a variable number of
> buttons (based on some input) and relate each button to a function call
> with a unique parameter value. For example:
>
> array = {1, 2, 3, 4, 5};
> Column[{Row[
> Table[Button[array[[n]], Print[n], ImageSize -> {50, 50}], {n,
> 1,Length[array], 1}]]}]
>
> Gives me five buttons, labeled 1-5, but each prints 'n' when clicked on.
> How would I change this to print the value of n?
>
> My best guess at this point is that there is a scoping issue with n, but I
> don't have enough Mathematica experience to know how to assign static
> values...
>
>
> Thanks
>