dispEM Helper function to print warning/errors string the warning/error to show. "WARNING: " is appended automatically if a warning throwErrors true if the function should throw an error (opt, default true) toList a cell array of items to list. If supplied, then the string will be printed followed by each element in toList. If it is supplied but empty then nothing is printed (opt, default {}) trimWarnings true if only a maximal of 10 items should be displayed in a given error/warning (opt, default true) Usage: dispEM(string,throwErrors,toList,trimWarnings)
0001 function dispEM(string,throwErrors,toList,trimWarnings) 0002 % dispEM 0003 % Helper function to print warning/errors 0004 % 0005 % string the warning/error to show. "WARNING: " is appended automatically 0006 % if a warning 0007 % throwErrors true if the function should throw an error (opt, default true) 0008 % toList a cell array of items to list. If supplied, then the 0009 % string will be printed followed by each element in 0010 % toList. If it is supplied but empty then nothing is 0011 % printed (opt, default {}) 0012 % trimWarnings true if only a maximal of 10 items should be displayed in 0013 % a given error/warning (opt, default true) 0014 % 0015 % Usage: dispEM(string,throwErrors,toList,trimWarnings) 0016 0017 if nargin<2 0018 throwErrors=true; 0019 end 0020 if nargin<3 0021 toList=[]; 0022 elseif isempty(toList) 0023 return; 0024 else 0025 toList=convertCharArray(toList); 0026 end 0027 if nargin<4 0028 trimWarnings=true; 0029 end 0030 if numel(toList)>10 && trimWarnings==true 0031 toList{10}=['...and ' num2str(numel(toList)-9) ' more']; 0032 toList(11:end)=[]; 0033 end 0034 if throwErrors==false 0035 errorText=['WARNING: ' string '\n']; 0036 else 0037 errorText=[string '\n']; 0038 end 0039 if ~isempty(toList) 0040 for i=1:numel(toList) 0041 errorText=[errorText '\t' toList{i} '\n']; 0042 end 0043 end 0044 if throwErrors==false 0045 %Escape special characters, required for fprintf 0046 errorText=regexprep(errorText,'(\\|\%|'')(\\n)$','\\$0'); 0047 fprintf([errorText '\n']); 0048 else 0049 throw(MException('',errorText)); 0050 end 0051 end