From help-octave-request at bevo dot che dot wisc dot edu Tue Jan 20 12:50:13 1998 Subject: exist gotcha? (was Re: Re: How to tell a function from a string) From: king at cogsci dot ucsd dot edu (Jonathan King) To: help-octave at bevo dot che dot wisc dot edu Cc: king at cogsci dot ucsd dot edu Date: Tue, 20 Jan 1998 10:49:32 -0800 I *think* I've got this text attributed correctly... John W. Eaton writes: > >On 20-Jan-1998, Jim Van Zandt wrote: >| >| I also thought the documentation could have been more helpful. >| Eventually I found this: >| >| - Built-in Function: exist (NAME) >| Return 1 if the name exists as a variable, 2 if the name (after >| appending `.m') is a function file in the path, 3 if the name is a >| `.oct' file in the path, or 5 if the name is a built-in function. >| Otherwise, return 0. >| >| and decided that "exist(NAME) > 1" was a reasonable test. (Can it >| ever return 4?) > >No, Octave's exist function doesn't currently return 4, but I'm not >sure what Matlab uses that return code for, so if you want to write >portable code, you might want to check that first. Matlab4.2 allegedly returns 4 if A is a compiled SIMULINK(tm) function. I say "allegedly" only because I haven't tried it. >| I'm now surprised to read JWE's reply: >| >| > Unfortunately, there isn't a good way to do this from Octave, though >| > you could probably wrap is_valid function from src/variables.cc in a >| > DEFUN easily enough. >| >| What's wrong with the above test? > >Uh, I just completely forgot about it. I see one other "gotcha", as suggested by the following octave session: mk26:~/matlab> octave Octave, version 2.0.9 (i586-pc-linux-gnu). Copyright (C) 1996, 1997 John W. Eaton. This is free software with ABSOLUTELY NO WARRANTY. For details, type `warranty'. octave.bin:1> exist("who") % this will return 5, like it should ans = 5 octave.bin:2> % here's a stupid typo and its parse error octave.bin:2> exist(who) parse error: >>> exist(who) ^ octave.bin:2> exist("who") % now this will fail... ans = 0 octave.bin:3> who % until you use it correctly... octave.bin:4> exist("who") % and now it works again ans = 5 I'm not sure what's going on here, but it looks funny. As far as I can tell, this only happens with built-ins; specifically, if you precede the octave session with "touch who.m" in a directory on your path, the parse error doesn't seem to block the correct operation of exist (in the sense that it will correctly return ans=2, since it notices the shadowing function). I think I stumbled across this first when I accidentally defined a variable called "save"...but I think that's all I want to say about that particular incident. jking