Matlab 'exist' returns 0 for a file that definitely exists!

Sanjay Manohar picture Sanjay Manohar · Oct 15, 2010 · Viewed 8.5k times · Source

I'm running Matlab 7.8.0 under Windows.

I am calling an external utility using dos() which creates a file in the current directory. I the file is created correctly, but it cannot be seen by exist or fopen, which return 0 and -1 respectively. The filename is correct!

>> pwd
ans = 
I:\

>> ls

file1.asc     file2.asc     file3.asc

>> exist('file1.asc')           % this file was there before
ans =
     2

>> exist('file2.asc')           % this file is newly created
ans =
     0

to confirm it's not an odd/problematic filename, I checked from a Cygwin shell:

/cygdrive/i/ $ if [ -f file2.asc ]; then echo "OK"; fi
OK

So the file is good. I tried renaming it

/cygdrive/i/ $ mv file2.asc test

and in Matlab

>> ls

file1.asc      file3.asc      test

>> exist('test')
ans =
     0

If I exit and restart Matlab it works fine. But I need to dynamically create the file and then access it!

Answer

nsanders picture nsanders · Oct 15, 2010

Very mysterious.

You could try:

  • The rehash command to see if that helps.
  • The two-argument version of exists: exist('foo.txt', 'file')