"Unable to read MAT-file" shared resources between MATLAB instances

7 views (last 30 days)
I have two instances of MATLAB running. The first instance is collecting data and saving it to a MAT-file. It collects data at a fixed interval and saves it to the .mat file after collecting a certain amount of data. The second instance is doing some calculations on the same MAT-file data. It is importing the data using the "matfile" command. The second file is running every few minutes.
Every so often I get the following error:
"Unable to read MAT-file C:\dataFile.mat. Try load -ASCII to read as text."
Later I am able to load that same MAT-file in MATLAB manually, or restart the script with no issues. This does not happen all the time and appears to be sporadic.
Any ideas what might be causing this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Jun 2017
One reason you may be receiving the error "Unable to read MAT-file" is because multiple instances of MATLAB running on the same machine are not inherently synchronized when accessing shared resources. If two instances of MATLAB are accessing the same data, there is a chance the data may become corrupted.
A possible workaround for this issue would be to create a "lock" file for each instance of MATLAB, and before performing any operations on the data, check to ensure the other instance's "lock" file doesn't exist. This would indicate if it is safe to read or write to that MAT-file.Attached is a pair of files, "writer.m" and "reader.m". These files demonstrate one possible workflow for creating these "lock" files.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!