Error using load: unable to read file

95 vues (au cours des 30 derniers jours)
ANDREW YOO
ANDREW YOO le 24 Fév 2022
Réponse apportée : Siraj le 30 Oct 2023
I tried loading a dataset but I am having errors.
The error message is following:
Error using load
Unable to read file 'C:\Users\###file location(omitted for my privacy)###\HC_DAY3_dataSet__30dB_time50s.mat'.
Input must be a MAT-file or an
ASCII file containing numeric data with same number of columns in each row.
Error in uiimport/runImportdata (line 469)
datastruct = load('-ascii', fileAbsolutePath);
Error in uiimport/gatherFilePreviewData (line 437)
[datastruct, textDelimiter, headerLines]= runImportdata(fileAbsolutePath, type);
Error in uiimport (line 259)
gatherFilePreviewData(fileAbsolutePath);
And when I tried loading the file again:
>> load('HC_DAY1_dataSet__30dB_time50s.mat')
Error using load
Unable to read MAT-file C:\Users\###file location###\HC_DAY1_dataSet__30dB_time50s.mat. Not a binary MAT-file. Try load -ASCII to read as text.
Does anyone know the solution? I just want to load the file. Thank you.
  2 commentaires
Jan
Jan le 24 Fév 2022
The message means, that the file has neither a valid MAT-file format nor is it a simple ASCII file.
What is the contents of the MAT file? How did you create it?
Pol Cardona Rubio
Pol Cardona Rubio le 20 Avr 2022
Modifié(e) : Pol Cardona Rubio le 20 Avr 2022
I have the same error.
When using matfile() inside a function inside a parfor loop it runs perfect at first, but if a stop and rerun I get this error and I can't recover the existing data of the .mat file. How is that possible?
In my case the .mat file is created once with an empty struct and then matfile(,writable) I add new data.

Connectez-vous pour commenter.

Réponses (1)

Siraj
Siraj le 30 Oct 2023
Hi!
It seems that you're having trouble loading a MAT file using the "load" function. Whenever you try to load the file, you receive an error message saying, "Error using load, unable to read file".
If you are unable to load a MAT file using the "load" function, one possible reason could be that you don't have the necessary read permission for the file. In that case, you can try saving the file to a different location and then attempt to read it again.
Alternatively, if saving the file to a different location doesn't work, it's possible that the MAT file itself is corrupt. However, you may still be able to recover the non-corrupt variables within the MAT file by using the “matfile” command.
To better understand this, you can refer to the following code snippet:
% Generate random data
data1 = rand(100, 3);
data2 = rand(50, 4);
data3 = rand(200, 2);
v = rand(6,100);
% Save data as MAT file
save('random_data.mat', 'data1', 'data2', 'data3');
save('test.mat','v');
% Create a matfile object to access the MAT file
m = matfile("random_data.mat");
% Retrieve the variables from the MAT file
Newdata1 = m.data1; % Access 'data1' variable
Newdata2 = m.data2; % Access 'data2' variable
Newdata3 = m.data3; % Access 'data3' variable
You can also use the “matfile” command to recover the 'non-corrupt' portions of individual variables in a MAT file. Here's an example code demonstrating this:
m = matfile('test.mat');
size(m,'v')
ans = 1×2
6 100
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:
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
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'.
Hope this helps.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by