How can I fill an array of images from inside a for loop?

2 views (last 30 days)
Hi all.
I'm trying to read a bunch of images of faces from a directory within the current directory into an accessible array I can use later on to display one of them randomly.
Here's what I've got so far:
FaceFolder = 'Faces';
if ~isdir(FaceFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', FaceFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(FaceFolder, '*.bmp');
bitmapFiles = dir(filePattern);
AllFaces=zeros(length(bitmapFiles), 638);
for k = 1:length(bitmapFiles)
baseFileName = bitmapFiles(k).name;
fullFileName = fullfile(FaceFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
currentim = imread(fullFileName);
AllFaces(k,:) = currentim;
end
I keep getting "Subscripted assignment dimension mismatch" errors for the AllFaces(k,:) = currentim; line.
638 is what I believe to be the size of one of the bitmap files (they're all the same dimensions) hence that number, so I tried using that to set up an array of zeros before the loop to fill it in but clearly I'm doing something wrong.
Anyone have any ideas?

Answers (1)

Evan
Evan on 17 Jun 2013
It looks like you're trying to assign a [nxm] matrix (the image) to a [1xm] row in AllFaces. Instead, try this:
AllFaces(:,:,k) = currentim;
  4 Comments
Evan
Evan on 17 Jun 2013
Edited: Evan on 17 Jun 2013
Oh, I didn't see above where you initialized "AllFaces" before your loop. That's the problem. Does changing that part to the below code work?
AllFaces = zeros(480,638,3,length(bitmapFiles))
Max
Max on 17 Jun 2013
Ah, sorry I miscopied something.
Here's the most recent iteration that still returns the error:
AllFaces=zeros(length(bitmapFiles), 480, 638, 3);
for k = 1:length(bitmapFiles)
baseFileName = bitmapFiles(k).name;
fullFileName = fullfile(FaceFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
currentim = imread(fullFileName);
AllFaces(k,:,:,:) = currentim;
end
The pre-allocated array's "length" column is aligned with the "k" column I'd like to fill which I think is right.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!