Why do I get "Error using load Cannot read file .." when trying to load a '.mat ' file on a different computer and how do I resolve it?
212 views (last 30 days)
MathWorks Support Team on 17 Oct 2019
I saved my MATLAB workspace to a MAT-file ('test.mat') on a computer (say 'computer 1') and transferred it to another computer (say 'computer 2') using a cloud storage service (for e.g. https://www.box.com/).
The saved mat file ('test.mat') loads without any issues on the computer it was created. But, when I try to load 'test.mat' in MATLAB on 'computer 2' using:
I receive the following error:\n
Error using load
Cannot read file
My questions are:
- Why can't I load the .mat file in MATLAB on computer 2? What is the underlying reason for the error?
- Is the MAT file corrupt?
- Can the data in the file be recovered somehow?
MathWorks Support Team on 30 Dec 2020
Assuming that the file was saved properly on 'computer 1', without any issues. The possible answers to your questions are as follows:
1) Why can't I load the .mat file in MATLAB on computer 2? What is the underlying reason for the error?
The fact that the MAT file loads fine on 'computer 1', one possibility is that the file may have been corrupted during the transfer to 'computer 2' using the cloud storage service. While there can be many reasons behind this, one way to verify this is to re-send the file to 'computer 2' using a different approach, (say using a USB flash drive / external hard drive/email / a different cloud storage service). Then try to load it in MATLAB on 'computer 2'. I have had a similar experience when transferring a large MAT file using Box (https://www.box.com/) corrupted the MAT file.
I would also suggest trying out MATLAB Drive for storing and transferring your MATLAB related files:
2) Is the MAT file corrupt?
The error suggests that the MAT file is probably corrupt and there is very little that can be done about it. Another indication of the MAT file being corrupt are error messages related to 'HDF5' appearing on the MATLAB Command Window / Linux or MacOS Terminal / Windows Command prompt.
HDF5-DIAG: Error detected in HDF5 (1.8.12) thread 0: #000: H5Dio.c line 179 in H5Dread(): can't read data major: Dataset minor: Read failed #001: H5Dio.c line 547 in H5D__read(): can't read data .....
3) Can the data in the file be recovered somehow?
You can try to recover the 'non-corrupt' portions of individual variables in the mat file by using the 'matfile' command (https://www.mathworks.com/help/matlab/ref/matlab.io.matfile.html). Consider for example that 'test.mat' has a variable 'v' that is unreadable. You can first get the size of this variable using the 'size' function as follows:
This tells us that the variable 'v' in 'test.mat' is a 2-D array that has 6 rows and 100 columns (a total of 600 values). You can then try to read the 'non-corrupt' values in 'v' and store them in a new array 'vRec' as follows:
m = matfile('test.mat'); size(m,'v') ans = 6 100
The above code will try to read all the 'non-corrupt' values in 'v' and store them in 'vRec'. The corrupt values in 'v' will be stored as 'NaNs'.
vRec = zeros(6,100); % pre-allocate for efficiency for i = 1:6 for j = 1:100 try vRec(i,j) = m.v(i,j); % store the value if it is not corrupt catch vRec(i,j) = NaN; % if the value is corrupt/unreadable store 'NaN' disp([int2str(i) ',' int2str(j) ' is unreadable.']); % display the indices of the unreadable elements end end end