From help-request at octave dot org Fri Jan 21 10:29:58 2005 Subject: Re: Octave's eval function From: "E. Joshua Rigler" To: David dot Bateman at motorola dot com Cc: Help-Octave Date: Fri, 21 Jan 2005 09:30:52 -0700 On Fri, 2005-01-21 at 02:50, David Bateman wrote: > E. Joshua Rigler wrote: > > >The following does what I want: > > > > out=eval("if (~exist('tmp')) tmp=0; endif; tmp=tmp+1; tmp*5;"); > > > >...specifically, it evaluates the string to 1) initialize the variable > >tmp if it doesn't already exist; 2) increment tmp by 1 each time it is > >called; and 3) return tmp*5 to the variable out. > > > >Is the fact that eval appears to always return the result of the last > >expression in the string (i.e., tmp*5) strictly intentional? I can't > >really tell from the help file. This is exactly what I want, but I just > >want to be sure this behavior isn't likely to change anytime soon. > > > >I'm running 2.1.64 with the latest Forge. Thanks, > > > >EJR > > > > > > Why do you need eval in the case above? It can be written directly > without eval. "eval" seems to be returning "ans", which would explain > your behaviour. > > I'd also be inclined to write this slightly differently as > > eval('tmp=tmp+1'; tmp*5;','tmp=0;'); out = ans; > > since, it doesn't seem that the CATCH string can return an argument in > octave 2.1.64 (perhaps this is a bug), and matlab can't return an > argument in either string. > > Regards > David What I am doing specifically is passing a cell array to a special state-space filter I've written. The contents of each cell can be a simple scalar coefficient, indicating time-stationary dynamics, a vector of predetermined time-dependent coefficients, or a string that can be evaluated to produce coefficients that are arbitrary functions of the current state (actually, they can be functions of anything that might exist in the current environment, including other filter coefficients). For the case above, "tmp" would be a proxy for time, and the output from eval would be the time-varying parameter. Obviously, in this example the coefficient is simply growing linearly as a function of time, and could easily be determined before-hand and passed as a vector. You can probably imagine cases where the time-varying coefficient is a function of the current state and eval really is necessary. Or maybe I'm just obsessing too much on keeping this algorithm overly generic ;^). In any case, I need to be able to place an entire algorithm in a single string that can be evaluated to manipulate the current environment, and return a usable result. I don't usually worry very much about ML compatibility, but since you've just explained that my solution definitely won't work in ML, maybe I'll implement your suggestion anyway, just in case I do decide to port it some day. Thanks. -EJR ------------------------------------------------------------- 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 -------------------------------------------------------------