Re: Variable amount of Buttons in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg92774] Re: Variable amount of Buttons in Mathematica
- From: ADL <alberto.dilullo at tiscali.it>
- Date: Mon, 13 Oct 2008 06:17:05 -0400 (EDT)
- References: <gcscrf$8l3$1@smc.vnet.net>
On 12 Ott, 10:34, SDY <young... at gmail.com> wrote:
> I'm pretty new to this...
>
> I'm trying to write a function that will create a variable number of butt=
ons (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,L=
ength[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 val=
ues...
>
> Thanks
By adding a With[...] clause, I could obtain the expected result:
array = {1, 2, 3, 4, 5};
Column[{Row[
Table[With[{n = n},
Button[array[[n]], Print[n], ImageSize -> {50, 50}]], {n,
Length[array]}]]}]
Regards
Alberto