index exceed matrix dimension
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Guillaume
el 5 de Sept. de 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 comentarios
Stephen23
el 5 de Sept. de 2018
Editada: Stephen23
el 5 de Sept. de 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 comentarios
Guillaume
el 5 de Sept. de 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
el 5 de Sept. de 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.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!