function Makemovie_3Dgriddeformations
%Program to make a movie of grid deformations from multiple 3D figures.
%Each figure opens one at a time and the user is prompted to click where he/she wants the figure label 
%to appear (for instance, click at the bottom left so that the label appears there on the movie – label position is user-determined separately for each loaded figure). 
%
%Next program to run: Deform_across_days 
%
days=7:19;

close all;
offset=50;%for placing figure labels

tmp=[days' days'];
tmp=tmp';
tmp=reshape(tmp,1,length(days)*2);
timepts=tmp(2:end-1);

colines=[0 0 0];
colbkg=[1 1 1];

Pathname=uigetdir(cd,'Select main project folder in  which all shape results and figures are stored');
trsfo=input('If you used Compute_deformation_patterns Press 1, if you used Compute_deformation_patterns_lwm press 2');
if trsfo==1
    load([Pathname '\Grid deformations in 2D\Meangrid_day_by_day_deform07-19.mat']); % we load this because predef and postdef mean outlines are stored there
    addname2='pol3';
else
    load([Pathname '\Grid deformations in 2D\Meangrid_day_by_day_deform_lwm07-19.mat']); % we load this because predef and postdef mean outlines are stored there
    addname2='lwm';
end

figdir=['3D figures of day to day grid deformations - for 3D movie' addname2];
figsf=[Pathname '\' figdir];

f=dir(fullfile(figsf,['*.' 'fig']));
nbfigs=size(f,1);

%figsf=uigetdir(pwd,['Select folder where figures are']);
cd(figsf);
%mpd=uigetdir(pwd,['Select folder to save movie to']);
%fnameout=input('Movie file name (without extension)','s');
fnameout='Movie_3Dgrid_deformations';
fna=[figsf '\' fnameout];
nbf=10;
aviobj=avifile([fna '.avi'],'FPS',nbf,'Compression','none');

for i=1:nbfigs
    uiopen([ '\Figure' num2str(i) '.fig'],1);
    gcf;
    hold on,
    axis on
    %hh1=title('click where you want the figure label to be');
    %[xd,yd]=ginput(1);
    %set(hh1,'Visible','off');
    hh1=get(gca);
    xd=hh1.XLim(1)+offset;
    yd=hh1.YLim(1)+offset;
    
    time=timepts(i);
    if rem(i,2)==0
        
        hh=text(xd,yd,0,['DAS' num2str(time) ' deformed from DAS' num2str(time-1)]);
    else
        hh=text(xd,yd,0,['DAS' num2str(time) ' before deformation']);
        h=line([0 0],[-500 500],[0 0]);
        set(h,'Color',colines,'LineWidth',[3]);
    end
    set(hh,'Color',[0 0 0]);
    %[xd,yd]=ginput(1);% for user to see figure only
    axis off
    axis vis3d
    %vals=input('give matrix for axes values [xmin xmax ymin ymax zmin zmax]');
    %vals=valstot(time-7+1,:);
    set(gca,'Color',colbkg);
    set(gcf,'Color',colbkg);
    
    %view(-39,30)
    %axis([-25 25 -20 20 -25 1])
    vals2=[0 -30 -30 -30 ;90 90 60 30]';
    for cc=1:4
        view(0,90);
        %axis([vals]);
        
        %camorbit(5,0,'camera')
        
        drawnow
        %set(gcf,'Position',[360 500 420 315])
        %set(gcf,'Position',[1281 1 1280 950])
        set(gcf,'Position',[1553 203 744 549]);
        axis off;
        % set(gcf, 'Renderer', 'zbuffer');
        frame=gcf;
        
        aviobj=addframe(aviobj,frame);
        
        
    end
    
    set(hh,'Visible','off');
    set(h,'Visible','off');
    
    cvals=cat(2,[0:10:90],[90:-10:0]);
    for cc=1:length(cvals)
        view(0-cvals(cc),90-cvals(cc));
        %axis([vals]);
        %camorbit(0,5,'camera')
        drawnow
        %set(gcf,'Position',[360 500 420 315])
        %set(gcf,'Position',[1281 1 1280 950])
        set(gcf,'Position',[1553 203 744 549]);
        axis off;
        % set(gcf, 'Renderer', 'zbuffer');
        frame=gcf;
        aviobj=addframe(aviobj,frame);
        
    end
    %
    % for cc=1:72
    %     %view(cc,30);
    %     axis([vals]);
    %     camorbit(5,0,'camera')
    %     drawnow
    %     %set(gcf,'Position',[360 500 420 315])
    %     %set(gcf,'Position',[1281 1 1280 950])
    %     set(gcf,'Position',[1553 203 744 549]);
    %     axis off;
    %     % set(gcf, 'Renderer', 'zbuffer');
    %     frame=gcf;
    %     aviobj=addframe(aviobj,frame);
    %
    % end
    
    for cc=1:4
        view(0,90);
        %axis([vals]);
        %camorbit(5,0,'camera')
        drawnow
        %set(gcf,'Position',[360 500 420 315])
        %set(gcf,'Position',[1281 1 1280 950])
        set(gcf,'Position',[1553 203 744 549]);
        axis off;
        % set(gcf, 'Renderer', 'zbuffer');
        frame=gcf;
        aviobj=addframe(aviobj,frame);
        
    end
    %close all
    %keyboard
    %time=init+1;
end
aviobj=close(aviobj);
close all;

disp(['Movie of day to day grid deformations in 3D was saved in ' figsf]);
disp('Scale bars in movie represent 1mm');