Is there a MATLAB function to check whether another application has a lock on a data file in MATLAB 7.8 (R2009a)?

I am using an application external to MATLAB to write to a data file. I have written MATLAB code that loads this file and processes the data. I would like my MATLAB code to check to make sure that the data file is not open in the other application before loading the data.

 Accepted Answer

You can use 'fileattrib' function to check whether you have the write access to a particular file. The attributes 'UserRead' and 'UserWrite' obtained by executing the command will indicate whether you have the read and write access to the file. Please note that the value '1' for a particular attribute indicates that you have access. Most applications that write to file will prohibit other applications from concurrently writing to that file, so the 'UserWrite' attribute should be a good indicator for checking whether or not the external application has completed writing to the file.
Please refer to the documentation link below for more information on 'fileattrib' function:
For example:
>>[stat,struc] = fileattrib('D:\work\results\mydatafile')
stat = 1struc = Name: 'D:\work\results\mydatafile' archive: 0 system: 0 hidden: 0 directory: 1 UserRead: 1 UserWrite: 1 UserExecute: 1 GroupRead: NaN GroupWrite: NaN GroupExecute: NaN OtherRead: NaN OtherWrite: NaN OtherExecute: NaN
The operation is successful as indicated by the status, stat, value of 1. The structure, struc, contains the file attributes.

2 Comments

Thank you for pointing out the error Alec, we’ve updated the answer to include your suggested change.
This article has implicit reliance on MS Windows that is not noted.
I am having difficulty confirming the given solution for MS Windows. I do find a hint that it might work in some cases, but finding reference material for that case is elusive due to "drowning in data" of discussions of cases where the proposed solution seems unlikely to work, especially if there is a network file system involved.
The fileattr technique is not going to work on OS-X or Linux. Locks are more often advisory on OS-X and Linux, and do not work by manipulating file permissions. Ability to open the file for the requested access would not usually be sufficient to tell you whether another process has the file open on those operating systems.
Detecting whether another process has a file open is not an easy task when networked filesystems are taken into account, especially if there might be some older operating systems in the mix.
Examining timestamps can help, but unfortunately timestamps are not updated until cache is flushed, at least not in MS Windows.
A more complete answer to the question is probably going to involve reference to

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2009a

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!