% Generic Saving 
%At first timestep do preliminaries
if k==0
	d0=fileparts([pwd,filesep]);
	d0=d0(1:strfind(d0,'trunk')+4);
    if isfield(app,'saveimg')
        if ~isempty(app.saveimg)
            if ~exist(sprintf('%s/Save/%s/',d0,app.saveimg),'dir')
                mkdir(sprintf('%s/Save/%s/',d0,app.saveimg)) 
            end           
        end
    end
    if isfield(app,'savemat')
        if isempty(app.savemat)
            d0=fileparts([pwd,filesep]);
            d0=d0(1:strfind(d0,'trunk')+4);
            if isfield(app,'S') && exist('ui','var')
                save(sprintf('%s/Save/mat/param.mat',d0),'P','Pi','ui','vi',...
                    'u','v','rho','rhoi','YYt','MEAN_ENER','EIGPLOT','T','Smax','XP',...
                    'YP','XU','YU','XV','YV','uid','vid','pid',...
                    'NodeP','Nodeu','Nodev','idsP','idsu','idsv','app','dx','dy');
            else
                save(sprintf('%s/Save/mat/param.mat',d0),'P',...
                    'u','v','rho','T','XP',...
                    'YP','XU','YU','XV','YV','uid','vid','pid',...
                    'NodeP','Nodeu','Nodev','idsP','idsu','idsv','app','dx','dy');
            end
            fprintf('Saving %s mat-file in %s/Save/mat/param.mat',...
                SetupScript,d0,k/app.PlotIntrvl);
        else
            if ~exist(sprintf('%s/Save/%s/',d0,app.savemat),'dir')
                mkdir(sprintf('%s/Save/%s/',d0,app.savemat))
            end
            d0=fileparts([pwd,filesep]);
            d0=d0(1:strfind(d0,'trunk')+4);
            if isfield(app,'S') && exist('ui','var')
                save(sprintf('%s/Save/%s/param.mat',d0,app.savemat),'P','Pi','ui','vi',...
                    'u','v','rho','rhoi','YYt','MEAN_ENER','EIGPLOT','T','Smax','XP',...
                    'YP','XU','YU','XV','YV','uid','vid','pid',...
                    'NodeP','Nodeu','Nodev','idsP','idsu','idsv','app','dx','dy');
            else
                save(sprintf('%s/Save/%s/param.mat',d0,app.savemat),'P',...
                    'u','v','rho','T','XP',...
                    'YP','XU','YU','XV','YV','uid','vid','pid',...
                    'NodeP','Nodeu','Nodev','idsP','idsu','idsv','app','dx','dy');
            end
            fprintf('Saving %s mat-file in %s/Save/%s/param.mat',...
                SetupScript,d0,app.savemat);
        end
    end
end

%At every timestep, save as necessary
if isfield(app,'saveimg')
    if isempty(app.saveimg)
        d0=fileparts([pwd,filesep]);
        d0=d0(1:strfind(d0,'trunk')+4);
        print('-djpeg85','-r200',...
            sprintf('%s/Save/img/%05d.jpg',d0,ceil(k/app.PlotIntrvl)));
        fprintf('Saving %s image in %s/Save/img/%05d.jpg\n',...
            SetupScript,d0,ceil(k/app.PlotIntrvl));
    else
        d0=fileparts([pwd,filesep]);
        d0=d0(1:strfind(d0,'trunk')+4);
        print('-djpeg85','-r200',...
            sprintf('%s/Save/%s/%05d.jpg',d0,app.saveimg,k/app.PlotIntrvl));
        fprintf('Saving %s image in %s/Save/%s/%05d.jpg\n',...
            SetupScript,d0,app.saveimg,ceil(k/app.PlotIntrvl));
    end
end

if isfield(app,'savemat')
    if isempty(app.savemat)
        d0=fileparts([pwd,filesep]);
        d0=d0(1:strfind(d0,'trunk')+4);
        if isfield(app,'S') && exist('ui','var')
            save(sprintf('%s/Save/mat/%05d.mat',d0,ceil(k/app.PlotIntrvl)),'P','Pi','ui','vi','u','v','rho','rhoi','YYt','MEAN_ENER','EIGPLOT','T','Smax','k');
        else
            save(sprintf('%s/Save/mat/%05d.mat',d0,ceil(k/app.PlotIntrvl)),'P','u','v','rho','T','k');            
        end
        fprintf('Saving %s mat-file in %s/Save/mat/%05d.mat\n',...
            SetupScript,d0,k/app.PlotIntrvl);
    else
        d0=fileparts([pwd,filesep]);
        d0=d0(1:strfind(d0,'trunk')+4);
        if isfield(app,'S') && exist('ui','var')
            save(sprintf('%s/Save/%s/%05d.mat',d0,app.savemat,ceil(k/app.PlotIntrvl)),'P','Pi','ui','vi','rhoi','u','v','rho','YYt','MEAN_ENER','EIGPLOT','T','Smax','k');
        else
            save(sprintf('%s/Save/%s/%05d.mat',d0,app.savemat,ceil(k/app.PlotIntrvl)),'P','u','v','rho','T','k');       
        end
        fprintf('Saving %s mat-file in %s/Save/%s/%05d.mat\n',...
             SetupScript,d0,app.savemat,ceil(k/app.PlotIntrvl));
    end
end