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