From help-octave-request at bevo dot che dot wisc dot edu Tue Jan 20 17:17:36 2004 Subject: Re: matlab nargin() function alternative in octave? From: Sharene Deanne Bungay To: taltman at lbl dot gov Cc: help-octave at bevo dot che dot wisc dot edu Date: Tue, 20 Jan 2004 18:14:11 -0500 Hi Tomer, Thank you for your suggestion. Unfortunately, using variable arguments will not solve my particular problem without significant rewrites of existing code. I may be able to shed more light on the subject with another example: Let's say for example that I have a large algorithm that calls a function as one of its steps. Now, let's say that I wish to try out two completely different implementations of this function, but that the two different implementations require different arguments, or even to have different global variables set to specific values. I don't want to make extensive changes to my algorithm, so instead, I just wish to test the number of arguments my function needs to take, and based on this, set up the required global variables. Something like, if (nargin('myfunc')==3) myglobal=3.45; endif I.e. I only would want to change myglobal to be 3.45 *IF* myfunc takes 3 arguments. So the issue is not how to handle a different number of arguments _within_ a function, but rather to tell how many arguments a function _expects_ to take, while outside the scope of that function. For a given run, the function itself is defined to take only say, 2 or 3 arguments (exclusive or). A quick hack would be to declare a global that merely stores a flag indicating what way our function is defined (say myflag=2 or myflag=3 for myfunc() defined for 2 or 3 arguments respectively) and then to change all of the nargin('myfunc')==3 checks to if (myflag==3). However, this doesn't answer my question as to if there is a way in octave to get the number of arguments expected by a defined function, outside the scope of that function. I hope this helps explain the issue a bit better. All ideas are appreciated! Cheers, Sharene On Tue, 2004-01-20 at 17:06, taltman at lbl dot gov wrote: > This looks like it can be handled using 'varargin'. It can allow your > function to accept a variable number of arguments. You'll just have to > do some variable type-checking. > > HTH, > > ~Tomer > > > > On Jan 20, 2004 at 10:04am, Sharene Bungay wrote: > > sbunga >Date: Tue, 20 Jan 2004 10:04:44 -0500 (EST) > sbunga >From: Sharene Bungay > sbunga >To: John W. Eaton > 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 09:04:48 -0600 > sbunga >Resent-From: help-octave at bevo dot che dot wisc dot edu > sbunga > > sbunga >Hi, thanks for your reply. > sbunga >I believe it can only accept string arguments under matlab. > sbunga >An example is as follows: Lets assume that I define > sbunga >an algorithm that uses regression as one of its steps. > sbunga >I code the algorithm to call reg(x1,x2,...) with the > sbunga >correct number of arguments. reg() in turn is conditionally > sbunga >defined to take either say, 2 or 3 arguments, depending > sbunga >on some user-set parameter. I could then have my algorithm > sbunga >test something like: > sbunga > > sbunga >if (nargin('reg')==3) > sbunga > sigma=0.001; > sbunga > reg(x1,x2,sigma); > sbunga >else > sbunga > reg(x1,x2); > sbunga >end > sbunga > > sbunga >Now if I were coding this, I would probably just have a global > sbunga >flag something like: > sbunga > > sbunga >if (userselect==reg1) > sbunga > regfunc1(x1,x2,sigma); > sbunga >else > sbunga > regfunc2(x1,x2); > sbunga >end > sbunga > > sbunga >But unfortunately, I need to use existing code that makes > sbunga >use of the nargin('string') functionality. > sbunga > > sbunga >TIA. > sbunga >Sharene. > sbunga > > sbunga >On Mon, 19 Jan 2004, John W. Eaton wrote: > sbunga > > sbunga >> On 19-Jan-2004, Sharene Deanne Bungay wrote: > sbunga >> > sbunga >> | I have some matlab code that I would very much like > sbunga >> | to use under octave. Unfortunately, the matlab code > sbunga >> | relies on matlab's ability to pass a function as an > sbunga >> | argument to nargin ( eg. nargin('sin')) and have it return > sbunga >> | the number of arguments expected by that function. > sbunga >> | I know that under octave, nargin is only a local scope scalar > sbunga >> | valid inside a given function, but does there exist a means > sbunga >> | under octave to get the number of expected arguments to a function > sbunga >> | (outside the scope of that function)? > sbunga >> | > sbunga >> | (This functionality is required since I have functions that are > sbunga >> | conditionally defined to take different arguments, and subsequent > sbunga >> | behaviour requires knowledge of the number of arguments. Rewriting > sbunga >> | the code to use global variables instead of relying on the form of > sbunga >> | the function definitions would be too extensive, so any help > sbunga >> | on this matter would be *GREATLY* appreciated.) > sbunga >> > sbunga >> Can you post an actual example that uses this functionality so we can > sbunga >> see how you are using it and why it is needed? > sbunga >> > sbunga >> The Matlab documentation shows that nargin and nargout may accept > sbunga >> string arguments. Are function handles also allowed? > sbunga >> > sbunga >> Thanks, > sbunga >> > sbunga >> jwe > 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 -------------------------------------------------------------