MATLAB: Changing the size info of images per iteration in a for loop

for loopimage sizeMATLAB

The situation: I'm running a for loop which anaylzes images. Each folder contains separate dataset and all are analyzed using a code that runs on subfolders within a parent folder. In some sub-folders, the Height and Width (H x W) of the images to be analyzed are different.
The problem: I get error whenever the code runs an iteration on a subfolder that contains images of different dimensions (H x W) compared to the sub-folder analyzed in the first iteration (k=1=True). What is missing here such that the code can be executed on images of different dimensions, smoothly?
Here is the relevent lines of the code I have, please read the comments:
for k = 1 : numberOfFolders;
if numberOfImageFiles >= 1600
%....some code
for s=InitFrameLast10:StartInt:LastFrameLast10;
for t=s:s+ImpFrames;
fullFileNameLast10=fullfile(thisFolder, baseFileNames{t});
fprintf(' Now reading %s\n', fullFileNameLast10);
imageArray_uncropLast10=imread(fullFileNameLast10);
FigInfo=imfinfo(fullFileNameLast10);
W=FigInfo.Width;
H=FigInfo.Height;
%I want to assigning W and H (which changes per iteration) to PupilBigLast10
PupilBigLast10(:,:,t)=imageArray_uncropLast10;
%The line above is where I get the error that Right (not changing--want it to be flexible) and left (changes per folder iteration) are not equal
%....some more code
end
end
%....more and more code

Best Answer

In MATLAB, it is not possible to create a 3D matrix with a different number of rows and columns in each slice. For such situations, we use cell arrays. You can do something like this:
Add this line at the place where you are initializing PupilBigLast10
PupilBigLast10 = {};
Then, replace the line
PupilBigLast10(:,:,t) = imageArray_uncropLast10;
with
PupilBigLast10{t} = imageArray_uncropLast10;