From help-octave-request at bevo dot che dot wisc dot edu Tue Jan 20 20:19:00 2004 Subject: Re: matlab nargin() function alternative in octave? From: taltman at lbl dot gov To: Sharene Deanne Bungay cc: help-octave at bevo dot che dot wisc dot edu Date: Wed, 21 Jan 2004 02:09:56 +0000 (UTC) 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 sbunga >To: TAltman at lbl dot gov sbunga >Cc: help-octave at bevo dot che dot wisc dot edu sbunga >Subject: Re: matlab nargin() function alternative in octave? sbunga >Resent-Date: Tue, 20 Jan 2004 17:17:02 -0600 sbunga >Resent-From: help-octave at bevo dot che dot wisc dot edu 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, taltman at lbl dot gov 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 sbunga >> sbunga >To: John W. Eaton sbunga >> sbunga >Cc: help-octave at bevo dot che dot wisc dot edu 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: help-octave at bevo dot che dot wisc dot edu 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 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 -------------------------------------------------------------