function linesout = parsestrace(filename)
%% PARSESTRACE(FILENAME)
%
% Read in FILENAME that is the output of STRACE and STATES commands within
% the Simulink Debugger.
%
% This Function will then locate all failed variable solver steps and
% identify which state caused the failure.
%
% The Simulink Debugger has its own state mapping that uses index numbers.
% The STATES command outputs the relationship between these index numbers
% and the normal complete Simulink representation of complete block path.
% This PARSESTRACE function parses that output, to build the mapping.
%
% As is also shown in this example, any other user defined mapping
% (we are still using block name as in example1) can then also be mapped
% to this existing information.
% Copyright 2004 - 2010 The MathWorks, Inc.
%% Read in file as a cell array
file = textread(filename,'%s','delimiter','\n','whitespace','');
%% Find State Mapping information
lines=regexp(file,'\([0-9]*:[0-9]*:[0-9]*'); % find all lines that have '(#:#:#' pattern
j=1;
for i=1:length(lines)
if (~isempty(lines{i})) % if not empty then pattern was found
[s,f,t] = regexp(file{i},'\([0-9]*:([0-9]*:[0-9]*)');
startind = t{1};
endind = startind(2);
startind = startind(1);
statemapping{j,1} = file{i}(startind:endind); % save off index info
[s,f,t] = regexp(file{i},'''([\w\/\s=\(\)\[\]\:]*)'''); % get block path
startind = t{1};
endind = startind(2);
startind = startind(1);
statemapping{j,2} = file{i}(startind:endind);
[s,f,t] = regexp(statemapping{j,2},'([^\/]*)*$'); % get block name
startind = t{1};
endind = startind(2);
startind = startind(1);
statemapping{j,3} = statemapping{j,2}(startind:endind);
j=j+1;
end
end
%% print state mapping to screen
statemapping
%% find all lines that have 'Failed' pattern
lines=regexp(file,'Fail');
%% Append State Name to Fail string
j=1;
for i=1:length(lines)
if (~isempty(lines{i})) % if not empty then pattern was found
[s,f,t,mstr,tstr] = regexp(file{i},'Ix = ([0-9]*)');
file{i} = [file{i} ' state ' statemapping{str2num(cell2mat(tstr{1}))+1,3}];
linesout{j} = file{i};
j=j+1;
end
end
% Pass out failed state info
if exist('linesout')
linesout = char(linesout');
else
disp(['No time step failed due to limiting of a state in ' filename])
linesout=[];
end