Why Do I get "Error using load Can't read file"?

37 views (last 30 days)
Hello all, I ran this code many times and have never received this result. I have read about it in: http://www.mathworks.com/matlabcentral/newsreader/view_thread/290218, and I even tried to add a clear at the end, but it doesn't seem to help. The Code that I have is:
dir_name = parms.dir_load_data;
dir_list = dir(strcat(dir_name,'\*.mat'));
file_names = {dir_list.name};
count = 1;
% enumerate on cells
for i = 1:length(file_names)
cd(parms.dir_load_data);
file_name = file_names{i};
dat = load(file_name);
i
Cell = dat.S;
clear dat;
clearvars -except parms i bSaveDataCells vec_age vec_arena_type file_names;
However when I get to file 38 it crashes, with:
"Error using load
Can't read file C:\Users\elib\Work\data\HD vs MD - For Blind
Rats\Squares\198-Cell_r11207_d140605_s01_t1_c5.mat.
Error in calc_HD_dist_not_absolute (line 45)
dat = load(file_name);"
Thanks in advance
  1 Comment
Walter Roberson
Walter Roberson on 23 Nov 2015
Please show the result of
exist(file_name)
dir(file_name)
Also, try
fid = fopen(file_name, 'r');
header = fread(fid,120).';
fclose(fid);
char(header)
header

Sign in to comment.

Accepted Answer

Eli Borodach
Eli Borodach on 23 Nov 2015
Seems the file is corrupt. Thanks anyway

More Answers (1)

Stephen23
Stephen23 on 23 Nov 2015
Edited: Stephen23 on 23 Nov 2015
That thread covers several different topics, and also advises that you should use fullfile (which your code does not) to provide the full file path to the function load. Actually your code has several features that should be changed:
  • Use fullfile instead of cd and strcat.
  • Change name of variable from Cell (too similar to cell).
  • Change variable from i (this is the imaginary unit).
It is much faster and more robust to avoid cd if you can, and probably more robust. The code below works without any error, for the several test .mat files that I created:
parms.dir_load_data = 'temp';
match_str = '*.mat';
file_struct = dir(fullfile(parms.dir_load_data,match_str));
file_names = {file_struct.name};
data = struct('S',cell(size(file_names)))
for k = 1:numel(file_names)
name = fullfile(parms.dir_load_data,file_names{k});
data(k) = load(name);
end
Note that this code stores the loaded data in a non-scalar structure.
  1 Comment
Walter Roberson
Walter Roberson on 23 Nov 2015
Yes, but I suspect a corrupted .mat file. Notice that it put in the full name in the error message even though only the relative name was given to load(). If it were a matter of the file not being found then the full name would not be generated in the error message. For the full name to have been generated, it had to find the file (though not necessarily in the current directory) and had some difficulty reading it. It did not give a permission denied message so I suspect the file is corrupted.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!