Matlab 'exist'返回一个确定存在的文件0 !

时间:2022-04-03 02:52:45

I'm running Matlab 7.8.0 under Windows.

我在Windows下运行Matlab 7.8.0。

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!

我使用dos()调用一个外部实用程序,它在当前目录中创建一个文件。我的文件是正确创建的,但它不能被exist或fopen看到,它们分别返回0和-1。文件名是正确的!

>> 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:

为了确认它不是一个奇数/有问题的文件名,我从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

在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!

如果我退出并重新启动Matlab,它可以正常工作。但我需要动态创建文件,然后访问它!

4 个解决方案

#1


7  

Very mysterious.

非常神秘。

You could try:

你可以试试:

  • The rehash command to see if that helps.
  • rehash命令查看是否有帮助。
  • The two-argument version of exists: exist('foo.txt', 'file')
  • 存在的双参数版本:exist('foo 'txt”、“文件”)

#2


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.

Matlab exist()命令不是简单的文件系统操作;它还会查看变量、函数等。因为您使用的是I:,我假设这是一个网络驱动器,您可能正在运行Jonas提到的目录内容缓存问题。

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

这里还有一些其他的解决方案,以防nsanders的双arg存在()或Jonas的更改通知修复对您不起作用。

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.

尝试使用到文件的绝对路径,比如“fopen('I:\file2.asc')”,而不是相对路径和pwd。Matlab将把不合格的文件名作为“部分路径”()和fopen()处理,并与目录信息缓存进行交互。Ls()不能处理部分路径,这可能是它可以看到文件而其他函数不能看到的原因。

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

您可以使用Matlab中的Java进行更简单的文件存在性测试。

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.

或者因为ls()命令显示了您想要的文件,您可以在ls之上实现文件存在检查。

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

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

必须使用“{}”来使ismember()在字符串级别而不是char级别上操作。

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.

如果您仍然无法阅读它,可以尝试在Matlab中使用Java进行较低层次的阅读。这将会告诉您,它是否是特定的Matlab的I/O函数,这些函数有问题,或者进程本身缺少对文件的访问。试试这个。如果你从中得到一个字符,这意味着你的Matlab。exe进程可以看到文件。

istr = java.io.FileInputStream('file2.asc')
c = char(istr.read())

#3


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.

在Windows上,我曾经在启动时收到过更改句柄通知警告,直到我关闭了警告。我现在手头没有7.8个,但默认情况下警告可能是关闭的。

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.

正如MathWorks站点上所解释的,如果Windows耗尽了更改通知句柄,它将无法正确地“感知”目录的内容是否发生了更改,这可能会导致您的问题。

#4


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.

您确定MATLAB的运行与explorer相同吗?如果MATLAB需要更高的权限才能运行,那么驱动器映射可能不同,您会发现I:\驱动器没有映射。

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

要解决这个问题,您需要以某种方式映射I:在较高权限下的驱动器。

#1


7  

Very mysterious.

非常神秘。

You could try:

你可以试试:

  • The rehash command to see if that helps.
  • rehash命令查看是否有帮助。
  • The two-argument version of exists: exist('foo.txt', 'file')
  • 存在的双参数版本:exist('foo 'txt”、“文件”)

#2


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.

Matlab exist()命令不是简单的文件系统操作;它还会查看变量、函数等。因为您使用的是I:,我假设这是一个网络驱动器,您可能正在运行Jonas提到的目录内容缓存问题。

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

这里还有一些其他的解决方案,以防nsanders的双arg存在()或Jonas的更改通知修复对您不起作用。

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.

尝试使用到文件的绝对路径,比如“fopen('I:\file2.asc')”,而不是相对路径和pwd。Matlab将把不合格的文件名作为“部分路径”()和fopen()处理,并与目录信息缓存进行交互。Ls()不能处理部分路径,这可能是它可以看到文件而其他函数不能看到的原因。

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

您可以使用Matlab中的Java进行更简单的文件存在性测试。

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.

或者因为ls()命令显示了您想要的文件,您可以在ls之上实现文件存在检查。

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

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

必须使用“{}”来使ismember()在字符串级别而不是char级别上操作。

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.

如果您仍然无法阅读它,可以尝试在Matlab中使用Java进行较低层次的阅读。这将会告诉您,它是否是特定的Matlab的I/O函数,这些函数有问题,或者进程本身缺少对文件的访问。试试这个。如果你从中得到一个字符,这意味着你的Matlab。exe进程可以看到文件。

istr = java.io.FileInputStream('file2.asc')
c = char(istr.read())

#3


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.

在Windows上,我曾经在启动时收到过更改句柄通知警告,直到我关闭了警告。我现在手头没有7.8个,但默认情况下警告可能是关闭的。

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.

正如MathWorks站点上所解释的,如果Windows耗尽了更改通知句柄,它将无法正确地“感知”目录的内容是否发生了更改,这可能会导致您的问题。

#4


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.

您确定MATLAB的运行与explorer相同吗?如果MATLAB需要更高的权限才能运行,那么驱动器映射可能不同,您会发现I:\驱动器没有映射。

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

要解决这个问题,您需要以某种方式映射I:在较高权限下的驱动器。