MATLAB: ERROR: “Assignment has more non-singleton rhs dimensions than non-singleton subscripts”

chardirerrorforfor loopstrcmpstring

Hello everyone, I am trying to debug an error in my code, but I am not sure what the problem is. I think it might be very simple, but I am not catching it. I have a list of .mat files in my current working directory, and I want to store specific file names in a variable "Incl". These file names begin with "PE". Here is my code:
DIR = dir;
STRCMP = 'PE';
for I = 1:length(DIR),
TF(I) = strncmpi(DIR(I).name,STRCMP,2);
end
%Logical 1 if PE file is found or 0 if PE file is not found
Ind = find(TF);
%Stores index location of each PE file (to be compared with DIR)
for J = 1:length(Ind)
Incl(J,1) = DIR(Ind).name;
end
My problem is during the last for loop. I am trying to call upon the specific file names in my directory that correspond to the index values I stored. These Dir(Ind).name variables are char format. I am trying to store them into Incl. Can anyone help me determine why I am getting this error?

Best Answer

I suggest:
for J = 1:length(Ind)
Incl{J,:} = DIR(Ind(J)).name;
end
Creating Incl as a cell with an open-ended column reference allows for the filenames to be more than one character long and also for their not all being the same length. (That may not be a problem for you but was a consideration in the code snippet I tested on my machine.) Also, you have to reference Ind by your loop index. (You would quickly have discovered that.)