Re: Passing options through user-defined functions
- To: mathgroup at smc.vnet.net
- Subject: [mg59013] Re: Passing options through user-defined functions
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Tue, 26 Jul 2005 04:03:54 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 7/25/05 at 1:30 AM, joe at eshu.net (Joe Christy) wrote:
>I have a function implemnted as module which uses various builtin
>functions. What is the best way to pass options to the builtin
>functions?
>For example, I have something like
>foo[p,q]:=
>Module[
> {bar},
> bar=Table[
> f[x,y],
> {x,1,p},{y,1,q}
> ];
> ArrayPlot[bar]
>]
>and I would like to be able to write something like
>foo[3,5, size -> 100]
>and see the results of ArrayPlot[bar, ImageSize->{3*100,5*100}]
Your specific example could be done as follows:
foo[p_,q_,opts___]:=
Module[
{bar},
bar=Table[
f[x,y],
{x,1,p},{y,1,q}
];
ArrayPlot[bar,ImageSize->size {p,q}/.opts]
]
You may also find it useful to define default options for foo using
Options[foo]={size->100}
Also if you use options frequently in your functions you will find the package Utilities`FilterOptions` useful. The FilterOptions package allows you to have options for your function as well as the called functions. Assuming this package is loaded your example could become
foo[p_,q_,opts___]:=
Module[
{bar},
bar=Table[
f[x,y],
{x,1,p},{y,1,q}
];
ArrayPlot[barImageSize->size {p,q}/.opts,
FilterOptions[ArrayPlot,opts]
]
]
With this version you can include any of the options for ArrayPlot as arguements to foo.
--
To reply via email subtract one hundred and four