function [MeanIm]=MeanImage(FilenamePrefix,StartIndex,StopIndex, Step)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FINDING THE MEAN OF A SERIES OF IMAGES
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% function [MeanIm]=MeanImage(FilenamePrefix,StartIndex,StopIndex, Step)
%
% This is a simple script - it loads a series of images with indices (and
% an optional step size) and finds the mean of them. If no step size is
% specified, then 1 is assumed
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First, test to see if we have the 4th argument. If not, then Step is set
% to 1
if (nargin ~= 4)
% Set test values
Step=1;
end
SummedImage=zeros(256,256);
NImages=0;
for k=StartIndex:Step:StopIndex
% For each k value,
% grab the corresponding file
% Can add leading zero if needed
if ((k<10)&(StopIndex>=10))
Filename=[FilenamePrefix '0' num2str(k) '.txt'];
else
Filename=[FilenamePrefix num2str(k) '.txt'];
end
Image=load(Filename);
SummedImage=SummedImage+Image;
% Also, count the images loaded
NImages=NImages+1;
end
% Find the mean image
MeanIm=SummedImage./NImages;