%import a set of images and convert to .mat format
%store in a 3D array of grayscale values

clear all
clc

%size of intended subset:
subset = 64;
%number of images:
image_num = 40;
%the filename to read (not with the number):
name_read = '20x_800_G600_E13_Z2_Middle';
%extension of image type:
ext = '.tif';
%exported .mat name (include number):
name_write = '20x_800_G600_E13_MiddleRU110_01';

%determine the size of zero-buffering needed in all 3 dimensions to make an
%even multiple of subset.
%create empty matrix size of desired .mat array with zero-buffering:
filename = sprintf(['20x_800_G600_E13_Z2_Middle',num2str(0),'.tif']);
Ifake = imread(filename);
[y,x] = size(Ifake);
DiffY = rem(subset,y);
DiffX = rem(subset,x);
DiffZ = rem(subset,image_num);
dec_img = zeros(y+DiffY,x+DiffX,image_num+DiffZ);

for K = DiffZ/2 : image_num+DiffZ/2
  filename = sprintf([name_read,num2str(K-DiffZ/2),ext]);
  I = zeros(y+DiffY,x+DiffX);
  Ifake = imread(filename);
  I((DiffY/2+1):(y+DiffY/2),(DiffX/2+1):(x+DiffX/2)) = Ifake;
  dec_img(:,:,K) = I;
  disp(K);
end

%save compiled data as:
save([name_write, '.mat']);

%You can check the final representation by plotting a layer of the .mat
%file. Simply double-click, and type: imshow(dec_img(:,:,i),[]), where i
%specifies the image you want to see. because of zero-buffering expect the
%first few to be fully black. The first visible one will be DiffZ/2.