Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

check file is open or not

11 views
Skip to first unread message

Vinay Lalwani

unread,
Dec 20, 2010, 12:30:29 AM12/20/10
to
Can anyone plz let me know how we can check if any file is already opened or not, in Matlab?

Walter Roberson

unread,
Dec 20, 2010, 1:25:33 AM12/20/10
to
On 19/12/10 11:30 PM, Vinay Lalwani wrote:
> Can anyone plz let me know how we can check if any file is already
> opened or not, in Matlab?

Is the question whether you have already have the file open *in the same
Matlab session* ? If so then fopen('all') will give you the file
identifiers of all open files, and then fopen() applied to each file
identifier will give you information about the file. However, the
filename returned would be the same as the filename specified to the
fopen() statement, which would not include any directory information if
it was fopen()'d with just a relative path name. In such a case, there
is no Matlab provided method to determine the directory involved.

If the question is how to determine whether the file is open by _some_
process, then for Linux or Mac OS, you would have to call a program to
do the work for you. It might have to be a privileged program (after
all, the file might happen to be one you do not have access to.) On
Windows, you could test to see if the file exists and what its creation
time was, then try to fopen() the file for reading, and see whether that
is permitted; if it is permitted then the file was [probably] not open
elsewhere. If the fopen() fails, then as a safety measure, check to see
whether the file still exists and what its current creation time is: if
the creation time was before you started the fopen() but the fopen()
failed, then either you do not have permission or else the file was
busy. Or possibly you might be able to extract a "busy" status from the
"message" returned if you use the two-output form of fopen().

Jos (10584)

unread,
Dec 20, 2010, 8:01:08 AM12/20/10
to
"Vinay Lalwani" <lalwan...@gmail.com> wrote in message <iempll$i79$1...@fred.mathworks.com>...

> Can anyone plz let me know how we can check if any file is already opened or not, in Matlab?

FYI, you can open the same file multiple times in ML:

fid1 = fopen('data.txt')
fid2 = fopen('data.txt')
fgetl(fid1)
fgetl(fid2) % returns the same line!
fclose all

So, perhaps, you should not bother too much about files being open in ML already. Or do you encounter specific problems?
As far as I know, there is no way ML can retrieve that information by itself.

Jos

Patrice Tscherrig

unread,
Oct 18, 2011, 3:39:30 AM10/18/11
to
"Jos (10584)" wrote in message <ienk2k$6vk$1...@fred.mathworks.com>...
Hi Jos

The problem might be if you want to write in a common txt file with 2 matlab sessions. The goal is to prevent this. Matlab does allow to allow in the same txt file in different sessions. would you know a way to prevent this?

Regards,
Patrice
0 new messages