-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddError.m
executable file
·327 lines (304 loc) · 8.45 KB
/
AddError.m
1
function DATA = AddError(DATA, varargin)%DATA = AddError(DATA, varargin) prints an error and adds it to DATA struct%passes varargin to sprintf, printf the resulting string in red, and adds% it to a cell string array DATA.errs%% DATA = AddError(DATA, otherDATA) copies errors from otherDATA to DATA% extra args lie '-prog' '-silent' MUST follow otherDATA%% DATA = AddError(DATA, otherDATA, str) adds str to DATA.errrs, but uses% otherData to determine expt/probe/other details%See also ShowErrors, PlotCommentssilent = 0;eid =0;pid = 0;skipvar = [];errfile = [];progname = '';if isfield(DATA,'verbose') if DATA.verbose < 0 return; elseif DATA.verbose == 0 silent =1; endendj = 1;while j <= length(varargin) if strcmp(varargin{j},'-silent') silent = 1; skipvar(end+1) = j; elseif strcmp(varargin{j},'-prog') %set program name skipvar(end+[1 2]) = [j j+1]; j = j+1; progname = varargin{j}; elseif strcmp(varargin{j},'-show') silent = 0; skipvar(end+1) = j; elseif strcmp(varargin{j},'-write') %add to a disk file skipvar(end+1) = j; j = j+1; skipvar(end+1) = j; errfile = varargin{j}; end j = j+1;endif isempty(varargin) if silent == 0 %show errors ShowErrors(DATA); else fprintf('AddError: No input args\n'); end return;enddDATA = DATA;if isstruct(varargin{1}) if length(varargin) -length(skipvar) == 1 DATA = CombineErrors(DATA, varargin{1},progname); return; else dDATA = varargin{1}; skipvar = [1 skipvar]; endendskiparg = varargin(skipvar);varargin = varargin(setdiff(1:length(varargin),skipvar));if iscellstr(varargin{1}) strs = varargin{1}; for j = 1:length(strs) DATA = AddError(DATA, strs{j}, skiparg{:}); end returnend s = sprintf(varargin{:}); s = strrep(s,'\','\\'); s = strrep(s,'\\\\','\\'); if isfield(DATA,'s') newerr.program = progname; newerr.s = s; DATA = CatStruct(DATA, newerr); if silent == 0 mycprintf('red','%s\n',s); end return; else if ~isfield(DATA,'errs') && ~isfield(DATA,'errmsg') DATA.errs = {}; end if ~isfield(DATA,'errdata') DATA.errdata = []; end if isfield(dDATA,'exptno') eid = dDATA.exptno; elseif isfield(dDATA,'suffix') eid = dDATA.suffix; else try eid = GetExptNumber(dDATA); end end if isfield(dDATA,'probe') pid = dDATA.probe; try eid = GetProbeNumber(dDATA); end end if isfield(dDATA,'program') newerr.program = dDATA.program; elseif isfield(dDATA,'progname') newerr.program = dDATA.progname; elseif ~isempty(progname) newerr.program = progname; end end newerr.s = s; if silent == 0 if isfield(newerr,'program') fprintf('%s: ',newerr.program); end mycprintf('red','%s\n',s); end if isfield(DATA,'errmsg') DATA.errmsg = {DATA.errmsg{:} s}; n = length(DATA.errmsg); elseif isstruct(DATA.errs) DATA.errs(end+1).msg = s; n = length(DATA.errs); else DATA.errs = {DATA.errs{:} s}; n = length(DATA.errs); end if isfield(DATA,'errdata') DATA.errdata(n).time = now; if eid > 0 DATA.errdata(n).expt = eid; end if pid DATA.errdata(n).probe = pid; end DATA.errdata = CopySFields(DATA.errdata,n,rmfield(newerr,'s'),'program'); end if ~isempty(errfile) if exist(errfile) X = load(errfile);%Error Lists are always structs now if isfield(X,'Errors') && iscell(X.Errors) E = CellToStruct(X.Errors); X.Errors = E; end else X.Errors = []; end newerr.time = now; newerr.eid = eid; if isfield(X,'Errors') if iscell(X.Errors) %shouldn't happen any more... X.Errors{end} = CopyFields(X.Errors{end},newerr); X.Errors{end+1}.s = newerr.s; else X.Errors(end+1).s = newerr.s; X.Errors = CopySFields(X.Errors,length(X.Errors),newerr); end elseif isfield(X,'errs') %older style for... X.errs{end+1}= newerr; end save(errfile,'-struct','X');end function OldShowErrors(DATA)if iscell(DATA) for j = 1:length(DATA) ShowErrors(DATA{j}); end return;elseif ischar(DATA) && isdir(DATA); name = [DATA '/Errors.mat']; if exist(name) X = my.load(name); ShowErrors(X); end return;end if isfield(DATA,'errs') idstr = []; [errs, id] = unique(DATA.errs); if isfield(DATA, 'errdata') errdata = DATA.errdata(id); else errdata = []; if isfield(DATA,'ex') && length(DATA.ex) == 1 idstr = sprintf('%sE%d',idstr,DATA.ex); end if isfield(DATA,'p') && length(DATA.p) == 1 idstr = sprintf('%sP%d',idstr,DATA.p); end end for j = 1:length(errs) if ~isempty(errdata) idstr = BuildErrIdstr(errdata(j)); end if ~isempty(idstr) fprintf('%s ',idstr); end cprintf('red','%s\n',deblank(errs{j})); endelseif isfield(DATA,'Errors') if isfield(DATA,'name') && isfield(DATA.Errors,'s') fprintf('Errors for %s\n',DATA.name); elseif isfield(DATA,'myload') && isfield(DATA.Errors,'s') fprintf('Errors for %s\n',DATA.myload.name); end ShowErrors(DATA.Errors); elseif isfield(DATA,'autofits') ShowErrors(DATA.autofits);elseif isfield(DATA,'s') for j = 1:length(DATA) idstr = BuildErrIdstr(DATA(j)); if ~isempty(idstr) fprintf('%s ',idstr); end cprintf('red','%s\n',DATA(j).s); endendif isfield(DATA,'errorlist') for j = 1:length(DATA.errorlist) ShowErrors(DATA.errorlist(j)); endendfunction idstr = BuildErrIdstr(E) idstr = ''; if isfield(E,'exptno') && ~isempty(E.exptno) idstr = sprintf('%sE%d',idstr,E.exptno); elseif isfield(E,'expt') idstr = sprintf('%sE%d',idstr,E.expt); end if isfield(E,'probe') idstr = sprintf('%sP%d',idstr,E.probe); end if isfield(E,'program') idstr = sprintf('%s:%s',idstr,E.program); elseif isfield(E,'progname') idstr = sprintf('%s:%s',idstr,E.progname); end if isfield(E,'time') %date when error recorded idstr = sprintf('%s:%s',idstr,datestr(E.time)); end if isfield(E,'t') && length(E.t) == 1 %should be time in sec idstr = sprintf('%s:%.1f',idstr,E.t./10000); end function DATA = CombineErrStruct(DATA, X, progname)if isfield(DATA,'errmsg') DATA.Errors = FixErrors(DATA);endif isfield(DATA,'Errors') && isfield(X,'Errors') DATA.Errors = CatStruct(DATA.Errors, X.Errors)elseif isfield(DATA,'Errors') if isfield(X,'errmsg') endendfunction DATA = CombineErrors(DATA, X, progname)if ~isfield(X,'errs') if isfield(X,'errmsg') X.errs = X.errmsg; elseif isfield(X,'Errors') DATA = CombineErrStruct(DATA, X, progname); return; else return; endendif isfield(X,'errdata') && ~isempty(progname) for j = 1:length(X.errdata) if ~isfield(X.errdata,'program') || isempty(X.errdata(j).program) X.errdata(j).program = progname; end endendif ~isfield(DATA,'errs') if isfield(DATA,'errmsg') DATA.errmsg = {DATA.errmsg{:} X.errs{:}}; DATA.errdata = CatStruct(DATA.errdata,X.errdata); elseif ~isempty(X.errs) DATA.errs = X.errs; if isfield(X,'errdata') DATA.errdata = X.errdata; else fprintf('AddError: No errdata\n'); end endelse if iscellstr(DATA.errs) && iscellstr(X.errs) DATA.errs = {DATA.errs{:} X.errs{:}}; if isfield(X,'errdata') && ~isempty(X.errdata) if isfield(DATA,'errdata') DATA.errdata = CatStruct(DATA.errdata,X.errdata); else DATA.errdata = X.errdata; end else end elseif isfield(X.errs,'errmsg') for j = 1:length(X.errs) DATA.errs{end+1} = X.errs(j).errmsg; DATA.errdata = CatStruct(DATA.errdata,rmfield(X.errs(j),'errmsg')); end endend