function create_retrieval_function(Directory,ProjectName,filename)
    if nargin<1
        Directory=pwd;
    end
    if nargin<2
        ProjectName='NewFig4';
    end
    if nargin<3
        filename='NewFig4Run.sh';
    end
    filelist={};
    h=fopen(filename);
    while ~feof(h)
        line=fgetl(h);
        if strcmp(line(1:4),'qsub')
            filelist{end+1}=line(5:end);
        end
    end
    fclose(h);
    % the program
    
    filename2=sprintf('Get%s.m',ProjectName);
    h=fopen(filename2,'w');
    fprintf(h,'function errors=%s(varargin)\n',filename2(1:end-2));
    fprintf(h,'%% arguments are optional morphogen, name pairs of strings\n');
    fprintf(h,'%% default is ''Morphogen'',''Polariser'' \n');
    moviedirectory=fullfile(ProjectName,'movies');
    
    % create movies directory
    fprintf(h,'mkdir(fullfile(''%s'',''movies''));\n',ProjectName);

    fprintf(h,'disp(''Moving data back will take some time'');\n')    ;
%     string2=sprintf('string=''pscp -r -pw %s "%s@escluster.uea.ac.uk:/gpfs/gpfs_esscratch01/sys/%s/%s/movies/%s*" "%s"''; ',...
%         getpassword,getyourclustername,getyourclustername,ProjectName,ProjectName,...
%         fullfile(Directory,ProjectName,'movies'));
%     fprintf(h,'%s\n',string2);
%     fprintf(h,'errors=unixcluster(string,''Copying movies directory back to windows '');\n');
    fprintf(h,'errors=copy_experiment_results_back(''%s'');\n',ProjectName);
%     fprintf(h,'if ~errors\n');
%     fprintf(h,'delete(''%s.done'');\n',filename);
%     string3=sprintf('string=''plink -pw %s %s@escluster.uea.ac.uk cd /gpfs/gpfs_esscratch01/sys/%s/%s; rm -r movies'';',...
%         getpassword,getyourclustername,getyourclustername,ProjectName);
%     fprintf(h,'%s\n',string3);
%     fprintf(h,'errors=unixcluster(string,''deleting results on cluster '');\n');
%     
%     for i=1:length(filelist)
%         string4=sprintf('string=''plink -pw %s %s@escluster.uea.ac.uk cd /gpfs/gpfs_esscratch01/sys/%s; rm %s'';',...
%             getpassword,getyourclustername,getyourclustername,filelist{i});
%         fprintf(h,'%s\n',string4);
%         fprintf(h,'errors=unixcluster(string,''deleting shell script %s on cluster '');\n',...
%             filelist{i});
%         % and delete the associated logfiles
%         string4=sprintf('string=''plink -pw %s %s@escluster.uea.ac.uk cd /gpfs/gpfs_esscratch01/sys/%s; rm %s.o*'';',...
%             getpassword,getyourclustername,getyourclustername,filelist{i});
%         fprintf(h,'%s\n',string4);
%         fprintf(h,'errors=unixcluster(string,''deleting shell script %s on cluster '');\n',...
%             filelist{i});
%     end
%     string4=sprintf('string=''plink -pw %s %s@escluster.uea.ac.uk cd /gpfs/gpfs_esscratch01/sys/%s; rm %s'';',...
%         getpassword,getyourclustername,getyourclustername,filename);
%     fprintf(h,'%s\n',string4);
%     fprintf(h,'errors=unixcluster(string,''deleting shell script %s on cluster '');\n',filename);
% 
%     fprintf(h,'end\n'); % end of if ~errors
%     fprintf(h,'if length(varargin)~=2\n');
%     fprintf(h,sprintf('VMSReport(''Project'',''%s'',''INVISIBLE'',true);\n',ProjectName));
%     fprintf(h,'else\n');
%     fprintf(h,sprintf('VMSReport(''Project'',''%s'',''Morphogen'',varargin{2});\n',ProjectName));
%     fprintf(h,'end\n');
    fprintf(h,'delete %s\n',filename2);
    fprintf(h,'end\n'); % end of generated function 
    fclose(h);
    disp(sprintf('Created %s file on your Windows computer',filename2))
end
