From help-request at octave dot org Fri Jan 21 03:50:26 2005 Subject: Re: Octave's eval function From: David Bateman To: "E. Joshua Rigler" Cc: Help-Octave Date: Fri, 21 Jan 2005 10:50:51 +0100 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 -- David Bateman David dot Bateman at motorola dot com Motorola CRM +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax) 91193 Gif-Sur-Yvette FRANCE The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary ------------------------------------------------------------- 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 -------------------------------------------------------------