0
votes

Matlab is giving me the error, "Subscripted assignment dimension mismatch" however I don't think there should be an issue. The code is below but basically I have a temp matrix that mimics the dimensions of another matrix, testData (actually a subset of it). I can assign the output of imread to the temp matrix but not to a subset of testData that has the same dimensions. I can even use the size function to prove they are the same dimensions yet one works and one doesn't. So I set temp = imread and then testData = temp and it works. But why should I have to do that?

fileNames = dir('Testing\*.pgm');
numFiles = size(fileNames, 1);
testData = zeros(32257, numFiles);
temp = zeros(32256, 1);

for i = 1 : numFiles,
  fileName = fileNames(i).name;

  % Extracts some info from the file's name and stores it in the first row
  testData(1, i) = str2double(fileName(6:7));

  % Here temp has the same dimensions as testData(2:end, i)
  % yet testData(2:end, i) = imread(fileName) doesn't work
  % however it works if I use temp as a "middleman" variable
  temp(:) = imread(fileName);
  testData(2:end, i) = temp(:);
end
1

1 Answers

0
votes

If the file that you're reading is a color image, imread returns an MxNx3 array. You can't assign a 3D array to a 1D vector without reshaping it, even if it contains the same number of elements. That's probably why you get the error when you try to assign the output of imread directly to testData. However, when you use an intermediate variable and collapse it into a column vector, the assignment works because now you're assigning a 1D vector to another 1D vector of equal size.

If you don't want to use an additional step, try this

testData(2:end,i)=reshape(imread(fileName),32256,1);