views:

59

answers:

4

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!

+3  A: 

Very mysterious.

You could try:

  • The rehash command to see if that helps.
  • The two-argument version of exists: exist('foo.txt', 'file')
nsanders
Thanks - the `rehash path` command solved the problem!
Sanjay Manohar
+1  A: 

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.

Kragen
good thought, thanks! - it wasn't the problem here though
Sanjay Manohar
+2  A: 

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
Thanks - I didn't get any warnings, but I expect that is the root of the problem. a call to `rehash` solved it.
Sanjay Manohar
+1  A: 

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

related questions