index exceed matrix dimension
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
Guillaume
am 5 Sep. 2018
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)
0 Kommentare
Stephen23
am 5 Sep. 2018
Bearbeitet: Stephen23
am 5 Sep. 2018
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 Kommentare
Guillaume
am 5 Sep. 2018
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
am 5 Sep. 2018
@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.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!