index exceed matrix dimension
Mostra commenti meno recenti
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
Risposte (2)
Guillaume
il 5 Set 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)
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 Commenti
@Sindhu: the code has a few features that are not very clear. Please help explain:
- There are two different directories, DataPath and dirName. Why does the first line of code check if a file with any .mat file exists in dirName, but then the code tries to delete a .mat from DataPath? Is that the intended behavior, that the code deletes files from a totally different folder than it checks in?
- The first line checks for .xlsx files and states that these will be deleted.. but does not actually do anything with .xlsx files. Is this the intended behavior?
- Why does the code check for the existence of any number of .mat, .xlsx, and .dat files, but then only delete the very first one using files{1}... ? Why not delete all of them?
It would help if you gave short explanation of the intended behavior, e.g.:
- in directory A: get a list of .dat files.
- in directory B: delete .mat and .xlsx files with the same names as the .dat files.
Guillaume
il 5 Set 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
il 5 Set 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.
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!