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

MATLAB

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.

Best 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 =
1
struc =
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.