%% Loading in a manipulting multi-image movies
clear 
close all

% First we have to go to the Matlab directory with all the test images,
% note, it might be different on your computer.
cd('C:\Program Files\MATLAB\R2016a\toolbox\images\imdata');

% Find all the files that start with AT3 in the filename, and count them
% with the length function
flist = dir('AT3*');
nfiles = length(flist);

% Switch back to original directory
cd('C:\Users\joelkralj\Documents\MATLAB')

% We don't know how big the image are, so the first thing we do is load in
% just the first image, even though there are 10 there.
img = im2double(imread(flist(1).name));
[ysize,xsize] = size(img);

% Now we have our first image in memory, so we can add additional frames
% onto that same variable.
for j = 2:nfiles
    img(:,:,j) = im2double(imread(flist(j).name));
end

% Now you can see that our img variable is a series of 2d images, where the
% 3rd dimension here is the time between frames.
size(img)

imwrite(img(:,:,1),'test_stack.tif');
for j = 2:nfiles
    imwrite(img(:,:,j),'test_stack.tif','writemode','append');
end



%% Showing and writing data
% If we want to display our movies, we need to use imshow in our loop,
% with the pause command.  Pause will pausee for however many seconds you
% provide.  Pause with no argument will wait for you to press a key.
figure
for j = 1:nfiles
    imshow(img(:,:,j))
    title(['Frame: ' num2str(j)])
    pause(.5) % Pause for 500 ms.
end

% There is also an implay comand wihch will give you a playback control
implay(img)





