index exceed matrix dimension

조회 수: 1 (최근 30일)
Emma
Emma 2018년 9월 5일
댓글: Stephen23 2018년 9월 5일
hii, please tell me , what is the reason for this error this is the error msg
Index exceeds matrix dimensions.
Error in convert (line 15)
if exist(strcat(DataPath, [files{1}(1:end-3) 'mat']), 'file')==2

답변 (2개)

Guillaume
Guillaume 2018년 9월 5일
Most likely, your files cell array is empty, hence the 1 files{1} exceeds the size of the array (0 = empty).
What is
size(files)

Stephen23
Stephen23 2018년 9월 5일
편집: Stephen23 2018년 9월 5일
files is empty, so accessing files{1} will throw an error. This is very fragile code that assumes that files contains at least one element, which clearly sometimes it does not.
Also the code is fragile as it assumes that the file extension has a particular length, which is a very faulty assumption that fails easily. The correct, robust solution is to split the file extension from the filename using fileparts.
  댓글 수: 3
Guillaume
Guillaume 2018년 9월 5일
or (more likely) files{1} has fewer than four elements.
Actually no, if files{1} has fewer than 4 elements, this won't cause an error. 1:end-3 will be an empty array but is still a valid index (which returns empty).
@Sindhu, Actually .... It's frustrating when people ask questions, get an answer (actually two) that tell them what the problem is, then come back with actually .... Give us the full question to start with!
As you've been told the reason fo the error is that file is empty. Most likely it's empty because there are no .dat file in the dirName folder, something we can't do anything about. To make that clearer, you could check for that after the dir call:
file = dir( fullfile(dirName,'*.dat'));
assert(~isempty(file), 'No dat file was found in: %s', dirName);
Stephen23
Stephen23 2018년 9월 5일
@Sindhu: I suspect that you really need to get rid of the hardcoded files{1} and use a loop. It does not make much sense otherwise.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by