function WriteErrorLog(err)

handles=guidata(findobj('Name','Muppet'));

fid=fopen('muppet.err','a');

datestring=datestr(datenum(clock),31);

usrstring='- Unknown user';
usr=getenv('username');

if size(usr,1)>0
    usrstring=[' - Generated by ' usr];
end

txt=['# Error Report - Muppet v' handles.MuppetVersion usrstring ' - ' datestring];
fprintf(fid,'%s \n',txt);

fprintf(fid,'%s\n','');

if isstruct(err)
    str=['Error: '  err.message];
    fprintf(fid,'%s\n',str);
    for i=1:length(err.stack)
        str=['An error occured in function: '  err.stack(i).name];
        fprintf(fid,'%s\n',str);
        str=['File: ' err.stack(i).file];
        fprintf(fid,'%s\n',str);
        str=['Line: ' num2str(err.stack(i).line)];
        fprintf(fid,'%s\n',str);
    end
else
    for i=1:length(err)
        fprintf(fid,'%s\n',err{i});
    end
end

fprintf(fid,'%s\n','');

fclose(fid);
