%% 
clear
close all

% Last time we saved our image as a .tiff stack.  Reading in a tiff stack
% is very similar to reading in multi images.  First we load in a single
% image

img = im2double(imread('test_stack.tif'));
nframes = 10; % I know this from before

% Note, this only read in the FIRST frame.  To load in the rest, we have to
% run a for loop, similar to before.  We have to add a second argument to
% the imread, which will be the frame we want to load in.

for j = 2:nframes
    img(:,:,j) = im2double(imread('test_stack.tif',j));
end
nframes = 10
% Play them out again to make sure everything looks good.
figure
for j = 1:nframes
    imshow(img(:,:,j))
    pause(.2)
end


% We can write these images to a new tiff stack if we want.  This is a
% little tricky, too.  You have to write the first image, and then append
% everything to the same file.
imwrite(img(:,:,1),'test_stack2.tif');
for j = 2:nframes
    imwrite(img(:,:,j),'test_stack2.tif','writemode','append');
end


% Also use google as your friend - matlab load tiff images