help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: matlab nargin() function alternative in octave?


From: Sharene Deanne Bungay
Subject: Re: matlab nargin() function alternative in octave?
Date: Wed, 21 Jan 2004 00:05:12 -0500

Hi Tomer,
Excellent point, and a very clean solution....if I had control
of the functional definitions so that I could call myfunc selectively
as myfunc1 and myfunc2. Unfortunately, my situation involves
a bunch of matlab code, where the existing third-party code is what
defines myfunc to be any one of several very different functions,
depending on what "module" is selected by the user. The rest of the code
then uses whatever function is defined in whichever one of these
modules, under a single name "myfunc". Thus, most of the code has
no idea a priori what "myfunc" actually looks like, but it knows
what to do given the number of arguments that myfunc() takes.
Since there are several modules (tens of them), all of which define
the same function name "myfunc", it's not practical for me to
rename them all as myfunc00, myfunc01,...etc, and then have a single
function myfunc() which handles the arguments as you suggest. Moreover,
many of the implementations of myfunc take the same number of arguments
while actually being different functions. The commonality only being
that those that take certain numbers of arguments need the same special
treatment. In this case, even the nice vararg solution you propose would
not be suitable. :( The nargin('myfunc') functionality makes such
module-baesd code fairly clean. So the desire is to make changes only
to the main algorithmic code, while not having to modify any of the
third party modules. I'd love to hear your thoughts on what options are
available for this situation.

Sharene

On Tue, 2004-01-20 at 21:09, address@hidden wrote:
> Hi Sharene,
> 
> Whether you do checks from the call-side, or you do checks from the
> called-function-side, you still must do a check.
> 
> To avoid using global variables, I'd advocate for a (message-)dispatch
> approach:
> 
> 1. have your main function that you want to test ("myfunc")
> 
> 2. "myfunc" will receive a variable number of arguments.
> 
> 3. based on the inputs, you dispatch the data to the correct version
>    of your real function ("myfunc1", "myfunc2")
> 
> I'd advocate this usage over modifying all the calls to this
> function. You won't need global variables, and you just need to modify
> one function. Plus, it's always good coding practice to check a
> function's inputs. Since you're checking the inputs anyways, why not
> put in some extra logic to take care of the two cases?
> 
> Just my $0.02,
> 
> ~Tomer
> 
> 
> 
> On Jan 20, 2004 at 6:14pm, Sharene Deanne Bungay wrote:
> 
> sbunga >Date: Tue, 20 Jan 2004 18:14:11 -0500
> sbunga >From: Sharene Deanne Bungay <address@hidden>
> sbunga >To: address@hidden
> sbunga >Cc: address@hidden
> sbunga >Subject: Re: matlab nargin() function alternative in octave?
> sbunga >Resent-Date: Tue, 20 Jan 2004 17:17:02 -0600
> sbunga >Resent-From: address@hidden
> sbunga >
> sbunga >Hi Tomer,
> sbunga >Thank you for your suggestion. Unfortunately, using variable arguments
> sbunga >will not solve my particular problem without significant rewrites of
> sbunga >existing code. I may be able to shed more light on the subject with
> sbunga >another example:
> sbunga >Let's say for example that I have a large algorithm that calls a
> sbunga >function as one of its steps. Now, let's say that I wish to try out
> sbunga >two completely different implementations of this function, but that
> sbunga >the two different implementations require different arguments, or even
> sbunga >to have different global variables set to specific values. I don't 
> want
> sbunga >to make extensive changes to my algorithm, so instead, I just wish
> sbunga >to test the number of arguments my function needs to take, and based 
> on
> sbunga >this, set up the required global variables. Something like,
> sbunga >if (nargin('myfunc')==3)
> sbunga >   myglobal=3.45;
> sbunga >endif
> sbunga >I.e. I only would want to change myglobal to be 3.45 *IF* myfunc 
> takes 3
> sbunga >arguments. So the issue is not how to handle a different number of
> sbunga >arguments _within_ a function, but rather to tell how many arguments
> sbunga >a function _expects_ to take, while outside the scope of that 
> function.
> sbunga >For a given run, the function itself is defined to take only say, 2 or
> sbunga >3 arguments (exclusive or). A quick hack would be to declare a global
> sbunga >that merely stores a flag indicating what way our function is defined
> sbunga >(say myflag=2 or myflag=3 for myfunc() defined for 2 or 3 arguments
> sbunga >respectively) and then to change all of the nargin('myfunc')==3 checks
> sbunga >to if (myflag==3). However, this doesn't answer my question as to if
> sbunga >there is a way in octave to get the number of arguments expected by a
> sbunga >defined function, outside the scope of that function.
> sbunga >
> sbunga >I hope this helps explain the issue a bit better.
> sbunga >All ideas are appreciated!
> sbunga >Cheers,
> sbunga >Sharene
> sbunga >
> sbunga >On Tue, 2004-01-20 at 17:06, address@hidden wrote:
> sbunga >> This looks like it can be handled using 'varargin'. It can allow 
> your
> sbunga >> function to accept a variable number of arguments. You'll just have 
> to
> sbunga >> do some variable type-checking.
> sbunga >> 
> sbunga >> HTH,
> sbunga >> 
> sbunga >> ~Tomer
> sbunga >> 
> sbunga >> 
> sbunga >> 
> sbunga >> On Jan 20, 2004 at 10:04am, Sharene Bungay wrote:
> sbunga >> 
> sbunga >> sbunga >Date: Tue, 20 Jan 2004 10:04:44 -0500 (EST)
> sbunga >> sbunga >From: Sharene Bungay <address@hidden>
> sbunga >> sbunga >To: John W. Eaton <address@hidden>
> sbunga >> sbunga >Cc: address@hidden
> sbunga >> sbunga >Subject: Re: matlab nargin() function alternative in octave?
> sbunga >> sbunga >Resent-Date: Tue, 20 Jan 2004 09:04:48 -0600
> sbunga >> sbunga >Resent-From: address@hidden
> sbunga >> sbunga >
> sbunga >> sbunga >Hi, thanks for your reply.
> sbunga >> sbunga >I believe it can only accept string arguments under matlab.
> sbunga >> sbunga >An example is as follows: Lets assume that I define
> sbunga >> sbunga >an algorithm that uses regression as one of its steps.
> sbunga >> sbunga >I code the algorithm to call reg(x1,x2,...) with the
> sbunga >> sbunga >correct number of arguments. reg() in turn is conditionally
> sbunga >> sbunga >defined to take either say, 2 or 3 arguments, depending
> sbunga >> sbunga >on some user-set parameter. I could then have my algorithm
> sbunga >> sbunga >test something like:
> sbunga >> sbunga >
> sbunga >> sbunga >if (nargin('reg')==3)
> sbunga >> sbunga >    sigma=0.001;
> sbunga >> sbunga >    reg(x1,x2,sigma);
> sbunga >> sbunga >else
> sbunga >> sbunga >    reg(x1,x2);
> sbunga >> sbunga >end
> sbunga >> sbunga >
> sbunga >> sbunga >Now if I were coding this, I would probably just have a 
> global
> sbunga >> sbunga >flag something like:
> sbunga >> sbunga >
> sbunga >> sbunga >if (userselect==reg1)
> sbunga >> sbunga >    regfunc1(x1,x2,sigma);
> sbunga >> sbunga >else
> sbunga >> sbunga >    regfunc2(x1,x2);
> sbunga >> sbunga >end
> sbunga >> sbunga >
> sbunga >> sbunga >But unfortunately, I need to use existing code that makes
> sbunga >> sbunga >use of the nargin('string') functionality.
> sbunga >> sbunga >
> sbunga >> sbunga >TIA.
> sbunga >> sbunga >Sharene.
> sbunga >> sbunga >
> sbunga >> sbunga >On Mon, 19 Jan 2004, John W. Eaton wrote:
> sbunga >> sbunga >
> sbunga >> sbunga >> On 19-Jan-2004, Sharene Deanne Bungay <address@hidden> 
> wrote:
> sbunga >> sbunga >> 
> sbunga >> sbunga >> | I have some matlab code that I would very much like
> sbunga >> sbunga >> | to use under octave. Unfortunately, the matlab code
> sbunga >> sbunga >> | relies on matlab's ability to pass a function as an
> sbunga >> sbunga >> | argument to nargin ( eg. nargin('sin')) and have it 
> return
> sbunga >> sbunga >> | the number of arguments expected by that function.
> sbunga >> sbunga >> | I know that under octave, nargin is only a local scope 
> scalar
> sbunga >> sbunga >> | valid inside a given function, but does there exist a 
> means
> sbunga >> sbunga >> | under octave to get the number of expected arguments to 
> a function
> sbunga >> sbunga >> | (outside the scope of that function)?
> sbunga >> sbunga >> | 
> sbunga >> sbunga >> | (This functionality is required since I have functions 
> that are
> sbunga >> sbunga >> | conditionally defined to take different arguments, and 
> subsequent
> sbunga >> sbunga >> | behaviour requires knowledge of the number of 
> arguments. Rewriting
> sbunga >> sbunga >> | the code to use global variables instead of relying on 
> the form of
> sbunga >> sbunga >> | the function definitions would be too extensive, so any 
> help
> sbunga >> sbunga >> | on this matter would be *GREATLY* appreciated.)
> sbunga >> sbunga >> 
> sbunga >> sbunga >> Can you post an actual example that uses this 
> functionality so we can
> sbunga >> sbunga >> see how you are using it and why it is needed?
> sbunga >> sbunga >> 
> sbunga >> sbunga >> The Matlab documentation shows that nargin and nargout 
> may accept
> sbunga >> sbunga >> string arguments.  Are function handles also allowed?
> sbunga >> sbunga >> 
> sbunga >> sbunga >> Thanks,
> sbunga >> sbunga >> 
> sbunga >> sbunga >> jwe
> sbunga >> sbunga >> 
> sbunga >> sbunga >
> sbunga >> sbunga >
> sbunga >> sbunga >
> sbunga >> sbunga 
> >-------------------------------------------------------------
> sbunga >> sbunga >Octave is freely available under the terms of the GNU GPL.
> sbunga >> sbunga >
> sbunga >> sbunga >Octave's home on the web:  http://www.octave.org
> sbunga >> sbunga >How to fund new projects:  
> http://www.octave.org/funding.html
> sbunga >> sbunga >Subscription information:  
> http://www.octave.org/archive.html
> sbunga >> sbunga 
> >-------------------------------------------------------------
> sbunga >> sbunga >
> sbunga >> sbunga >
> sbunga >> 
> sbunga >> 
> sbunga >> 
> sbunga >> -------------------------------------------------------------
> sbunga >> Octave is freely available under the terms of the GNU GPL.
> sbunga >> 
> sbunga >> Octave's home on the web:  http://www.octave.org
> sbunga >> How to fund new projects:  http://www.octave.org/funding.html
> sbunga >> Subscription information:  http://www.octave.org/archive.html
> sbunga >> -------------------------------------------------------------
> sbunga >
> sbunga >
> sbunga >
> sbunga >-------------------------------------------------------------
> sbunga >Octave is freely available under the terms of the GNU GPL.
> sbunga >
> sbunga >Octave's home on the web:  http://www.octave.org
> sbunga >How to fund new projects:  http://www.octave.org/funding.html
> sbunga >Subscription information:  http://www.octave.org/archive.html
> sbunga >-------------------------------------------------------------
> sbunga >
> sbunga >
> 
> 
> 
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]