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

조회 수: 34 (최근 30일)
Eli Borodach
Eli Borodach 2015년 11월 23일
편집: Stephen23 2015년 11월 23일
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
Walter Roberson
Walter Roberson 2015년 11월 23일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Eli Borodach
Eli Borodach 2015년 11월 23일
Seems the file is corrupt. Thanks anyway

추가 답변 (1개)

Stephen23
Stephen23 2015년 11월 23일
편집: Stephen23 2015년 11월 23일
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
Walter Roberson
Walter Roberson 2015년 11월 23일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by