As you found out already, lastwarn contains the last message only. You could overload the function warning to create a history of messages:
function varargout = warning(varargin)
persistent MsgList
if isa(MsgList, 'double')
MsgList = {};
end
if nargin == 2 && isempty(varargin{1}) && isa(varargin{1}, 'double')
switch varargin{2}
case 'GetList'
varargout{1} = MsgList;
return;
case 'ResetList'
MsgList = {};
return;
end
varargout = cell(1, nargout);
[varargout{:}] = builtin('warning', varargin{:});
MsgList{length(MsgList) + 1} = lastwarn;
This adds new messages for commands like warning('off', 'backtrace') also, so much more details are required for a smart behavior. But this demonstrates the general idea.
[EDITED] I've replaced the method to obtain and clear the list of messages. Using a magic string method in the first input is not 100% clean. Now this is used for controlling:
warning([], 'GetList')
warning([], 'ClearList')
Although it is very unlikely that somebody uses the warning message '$GetList', it might be a trap
Best Answer