
% axis tight manual % this ensures that getframe() returns a consistent size
filename = 'Ingranamento_helical_mean.gif';
tstep=1/2;
% colore
% color_pinion= 'c';
% color_gear = [56/255,124/255,234/255];
% trasparenza
% alpha_pinion=0.2;
% alpha_gear=0.2;
% opzione per plottare le mesh o no
% plot_mesh=0;
figh=figure(101);

% m=max(max(max(scaleZ1(:,:),scaleZ2(:,:))));
% ScaleZ_lim=[min(min(min(scaleZ1)),min(min(scaleZ2))),max(max(max(scaleZ1)),max(max(scaleZ2)))];
m=max(max(max(SigmaVM1(:,:),SigmaVM2(:,:))));
Sigma_lim=[min(min(min(SigmaVM1)),min(min(SigmaVM2))),max(max(max(SigmaVM1)),max(max(SigmaVM2)))];
for n = 1:numpos
   clear figure
    % plotto rispetto alla massima pressione totale tra tutte le posizioni
    % angolari
%      pnorm=(reshape(displ13(:,:,n),[],input4fem.params.sel_teeth))...
%          /min(max(displ13(:,:,:)));

     
%      PlotDefoMesh_mod3(figh,g1.FEM.nodes(:,2:4,n),g1.FEM.elems(:,2:9),m,...
%     displ13(:,:,n),scaleZ1(:,n),...
%     g1.FEM.surffaces,g1.FEM.elfa,ScaleZ_lim)
%       c=colorbar;
%      c.Label.String='[mm]';
%      PlotDefoMesh_mod3(figh,g2.FEM.nodes(:,2:4,n),g2.FEM.elems(:,2:9),m,...
%     displ23(:,:,n),scaleZ2(:,n),...
%     g2.FEM.surffaces,g2.FEM.elfa,ScaleZ_lim)
%      c=colorbar;
%      c.Label.String='[mm]';
% 
   PlotDefoMesh_mod3(figh,g1.FEM.nodes(:,2:4,n),g1.FEM.elems(:,2:9),m,...
    displ13(:,:,n),SigmaVM1(:,n),...
    g1.FEM.surffaces,g1.FEM.elfa,Sigma_lim)
     c=colorbar;
     c.Label.String='[MPa]';
      PlotDefoMesh_mod3(figh,g2.FEM.nodes(:,2:4,n),g2.FEM.elems(:,2:9),m,...
    displ23(:,:,n),SigmaVM2(:,n),...
    g2.FEM.surffaces,g2.FEM.elfa,Sigma_lim)
     c=colorbar;
     c.Label.String='[MPa]';

    if n == 1   
    keyboard % in modo da manipolare la visuale come si vuole
    end
    pause(0.5)
    % Capture the plot as an image
    frame = getframe(figh);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256,'nodither');
    % Write to the GIF File
    if n == 1
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',tstep);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',tstep);
    end
    clf(figh)
end