# Problem with exist

 Classic List Threaded
7 messages
Reply | Threaded
Open this post in threaded view
|

## Problem with exist

 I'm having a problem with exist(). If foo.m is currently loaded then exist('foo.anything') is true, regardless of whether the file exists. This is a problem, because my code does function out=foo();    if exist('foo.mat');       load('foo.mat');    else       out=calculate_out();       save('foo.mat',out);    end This example shows the problem \$ cat > foo.m function x=foo(y); x=y+1; endfunction; \$ octave -q octave:1> exist('foo') ans = 2 octave:2> exist('foo.mat') ans = 0 octave:3> foo(1); octave:4> exist('foo') ans = 2 octave:5> exist('foo.mat') ans = 2 octave:6> exist('foo.anything') ans = 2 Andy
Reply | Threaded
Open this post in threaded view
|

## Problem with exist

 On 22-Feb-2003, Andy Adler <[hidden email]> wrote: | I'm having a problem with exist(). | | If foo.m is currently loaded then | exist('foo.anything') is true, regardless | of whether the file exists. | | This is a problem, because my code does | | function out=foo(); |    if exist('foo.mat'); |       load('foo.mat'); |    else |       out=calculate_out(); |       save('foo.mat',out); |    end | | This example shows the problem | | \$ cat > foo.m | function x=foo(y); x=y+1; endfunction; | | \$ octave -q | octave:1> exist('foo') | ans = 2 | octave:2> exist('foo.mat') | ans = 0 | octave:3> foo(1); | octave:4> exist('foo') | ans = 2 | octave:5> exist('foo.mat') | ans = 2 | octave:6> exist('foo.anything') | ans = 2 Please try the following patch. Thanks, jwe 2003-02-22  John W. Eaton  <[hidden email]>         * variables.cc (symbol_exist): For names like foo.bar, don't omit         the part after the dot. Index: src/variables.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/variables.cc,v retrieving revision 1.246 diff -u -r1.246 variables.cc --- src/variables.cc 20 Feb 2003 20:45:49 -0000 1.246 +++ src/variables.cc 22 Feb 2003 18:19:59 -0000 @@ -559,10 +559,13 @@      if (sr && sr->is_defined ())      { +      bool not_a_struct = struct_elts.empty (); +      bool var_ok = not_a_struct || sr->is_map_element (struct_elts); +        if (! retval +  && var_ok    && (type == "any" || type == "var") -  && sr->is_variable () -  && (struct_elts.empty () || sr->is_map_element (struct_elts))) +  && sr->is_user_variable ())   {    retval = 1;   } @@ -570,21 +573,22 @@        if (! retval    && (type == "any" || type == "builtin"))   { -  if (sr->is_builtin_function ()) +  if (not_a_struct && sr->is_builtin_function ())      {        retval = 5;      } -  else if (sr->is_builtin_variable ()) +  else if (var_ok && sr->is_builtin_variable ())      {        retval = 101;      } -  else if (sr->is_builtin_constant ()) +  else if (var_ok && sr->is_builtin_constant ())      {        retval = 102;      }   }          if (! retval +  && not_a_struct    && (type == "any" || type == "file")    && (sr->is_user_function () || sr->is_dld_function ()))   {
Reply | Threaded
Open this post in threaded view
|

## Re: Problem with load

 In reply to this post by Andy Adler I was about to submit a different patch for my previous post, but John beat me to it! My problem now is that load(), only looks in the current directory (unlink Matlab, which searches the LOADPATH). I recommend the following patch to fix this: *** src/load-save.cc    Sat Feb 22 13:31:12 2003 --- src/load-save.cc.orig       Sat Feb 22 11:39:30 2003 *************** *** 3352,3358 ****       }     else       { !       std::string fname = file_ops::tilde_expand (file_in_path(argv[i],""));         if (format == LS_UNKNOWN)         format = get_file_format (fname, orig_fname); --- 3352,3358 ----       }     else       { !       std::string fname = file_ops::tilde_expand (argv[i]);         if (format == LS_UNKNOWN)         format = get_file_format (fname, orig_fname); Andy
Reply | Threaded
Open this post in threaded view
|

## Re: Problem with load

 On 22-Feb-2003, Andy Adler <[hidden email]> wrote: | I was about to submit a different patch for | my previous post, but John beat me to it! | | My problem now is that load(), only looks in the current | directory (unlink Matlab, which searches the LOADPATH). | | I recommend the following patch to fix this: | | *** src/load-save.cc    Sat Feb 22 13:31:12 2003 | --- src/load-save.cc.orig       Sat Feb 22 11:39:30 2003 | *************** | *** 3352,3358 **** |       } |     else |       { | !       std::string fname = file_ops::tilde_expand (file_in_path(argv[i],"")); | |         if (format == LS_UNKNOWN) |         format = get_file_format (fname, orig_fname); | --- 3352,3358 ---- |       } |     else |       { | !       std::string fname = file_ops::tilde_expand (argv[i]); | |         if (format == LS_UNKNOWN) |         format = get_file_format (fname, orig_fname); I think we should only do this for .mat files, and then only because it is a compatible thing to do, not because it is necessarily a good idea.  The problem (as I see it) is that it can produce unexpected results depending on what old junk files happen to be scattered around the load path.  It seems to me that if you want to load a file, you should probably be asking for exactly the file you want.  But maybe other people feel differently?  If so, what are the reasons (other than compatibility) that this feature is a good idea? BTW, when you make diffs, please use   diff -c old-file new-file so the sequence of changes is old followed by new, additions are marked with "+" and deletions are marked with "-".  It makes them much easy to read. Thanks, jwe
Reply | Threaded
Open this post in threaded view
|

## Re: Problem with load

 On Sat, 22 Feb 2003, John W. Eaton wrote: > It seems to me that if you want to load a file, you > should probably be asking for exactly the file you want.  But maybe > other people feel differently?  If so, what are the reasons (other > than compatibility) that this feature is a good idea? Here is my reason: if exist('processed_data.mat')    load processed_data.mat else    processed_data = recalculate_from_raw_data(); end Right now exist() doesn't tell me where it is, so there is no way I can issue the load command. The fixes are: 1) have a way to know where it is, or 2) have load pick it up automatically. Matlab compatibility pushes for #1. On the other hand, this could be a switch to load. Andy
Reply | Threaded
Open this post in threaded view
|

## Re: Problem with load

 On 22-Feb-2003, Andy Adler <[hidden email]> wrote: | On Sat, 22 Feb 2003, John W. Eaton wrote: | > It seems to me that if you want to load a file, you | > should probably be asking for exactly the file you want.  But maybe | > other people feel differently?  If so, what are the reasons (other | > than compatibility) that this feature is a good idea? | | Here is my reason: | | if exist('processed_data.mat') |    load processed_data.mat | else |    processed_data = recalculate_from_raw_data(); | end | | Right now exist() doesn't tell me where it is, so | there is no way I can issue the load command. | | The fixes are: 1) have a way to know where it is, or | 2) have load pick it up automatically. | Matlab compatibility pushes for #1. | | On the other hand, this could be a switch to load. Couldn't you use file_in_loadpath to find the file?  It returns an empty matrix if it doesn't find the file you are looking for.  Also, you can use the optional argument "all" to find all the files with the given name.  In that case, you could also warn about possible problems due to duplicates. jwe
Reply | Threaded
Open this post in threaded view
|

## Re: Problem with load

 On Mon, 24 Mar 2003, John W. Eaton wrote: > On 22-Feb-2003, Andy Adler <[hidden email]> wrote: > > | On Sat, 22 Feb 2003, John W. Eaton wrote: > | > It seems to me that if you want to load a file, you > | > should probably be asking for exactly the file you want.  But maybe > | > other people feel differently?  If so, what are the reasons (other > | > than compatibility) that this feature is a good idea? > | > | Here is my reason: > | > | if exist('processed_data.mat') > |    load processed_data.mat > | else > |    processed_data = recalculate_from_raw_data(); > | end > | > | Right now exist() doesn't tell me where it is, so > | there is no way I can issue the load command. > | > | The fixes are: 1) have a way to know where it is, or > | 2) have load pick it up automatically. > | Matlab compatibility pushes for #1. > | > | On the other hand, this could be a switch to load. > > Couldn't you use file_in_loadpath to find the file?  It returns an > empty matrix if it doesn't find the file you are looking for.  Also, > you can use the optional argument "all" to find all the files with the > given name.  In that case, you could also warn about possible problems > due to duplicates. That's a good idea. I didn't think of file_in_loadpath. Could we at least change the help documentation to give an example with file_in_loadpath to get Matlab compatible behaviour. For example:    Unlike the equivalent Matlab command, octave will not search    the LOADPATH to find the file. If such behaviour is desired,    then the following approach may be used:       filepath= file_in_loadpath( file );       if ~isempty( filepath );          load(filepath);       else          error(["Can't file ', file, ' in LOADPATH'])       end Andy