5

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!

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
  • 2
    I don't know Matlab, but it sounds like you could be running up against a problem of cached filesystem information. – Andrew Cooper Oct 15 '10 at 00:51
  • 1
    I don't have 7.8 installed but I tried this in R2007b (7.5) and R2009b (7.9) and it works on both. I opened MATLAB first and then created a file using the `edit` command in a DOS window in MATLAB's current directory. `exist` returns 2 immediately after the file is created. – Praetorian Oct 15 '10 at 01:13
  • I had similar issue as you did, but in my case I was trying to use a wildcard to check any similar file existing: `exist('~/hi*.txt','file')` and that was failing. Kind of similar to Andrew's answer and this [one](http://www.mathworks.com/matlabcentral/answers/182583-checking-the-existence-of-any-file-with-a-specific-extension-in-a-directory), I did `~isempty(dir('~/hi*.txt'))` – alexey Feb 02 '16 at 21:23

4 Answers4

8

Very mysterious.

You could try:

  • The rehash command to see if that helps.
  • The two-argument version of exists: exist('foo.txt', 'file')
nsanders
  • 12,250
  • 2
  • 40
  • 47
6

The Matlab exist() command is not a simple filesystem operation; it also looks at variables, functions, etc. Since you're on I:, I'm assuming that's a network drive, and you're probably running in to the dir contents caching problem that Jonas mentions.

Here are a couple other workarounds, in case nsanders' two-arg exist() or Jonas' change notification fixes don't work for you.

Try using absolute paths to the files, like "fopen('I:\file2.asc')", instead of relative paths and pwd. Matlab will treat unqualified filenames as "partial paths" for both exist() and fopen(), and that interacts with the directory info caching. Ls() does not work with partial paths, which may be why it can see the file and the other functions can't.

You can use Java from within Matlab to do a simpler file existence test.

java.io.File('file2.asc').exists()

Or since the ls() command is showing the file you want, you can just implement the file existence check on top of ls.

ismember({'file2.asc'}, ls())

The "{ }" is necessary to make ismember() operate at the string level instead of the char level.

If you're still having trouble reading it, try doing a lower level read with Java from within Matlab. That will tell you whether it's specifically Matlab's I/O functions that are having trouble, or of the process itself lacks access to the file. Try this. If you get a char out of it, that means your Matlab.exe process can see the file.

istr = java.io.FileInputStream('file2.asc')
c = char(istr.read())
Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
2

On Windows, I used to get change handle notification warnings at startup until I turned the warnings off. I don't have 7.8 at hand right now, but the warning may be off by default.

As explained on the MathWorks site, if Windows runs out of change notification handles, it will not be able to properly "sense" whether the content of a directory has changed, which might be causing your problems.

Jonas
  • 74,690
  • 10
  • 137
  • 177
1

Are you sure that MATLAB is running as the same user as explorer is? If MATLAB requires elevated permissions to run then the drive mappings may be different and you could find that the I:\ drive is not mapped.

To fix this you would need to somehow map the I: drive under elevated permissions.

Justin
  • 84,773
  • 49
  • 224
  • 367