%%

fid = fopen('../mem_analysis.csv','r');

% lines = readlines('../mem_analysis.csv','EmptyLineRule','skip');

names = {}; 
% Nframes = length(lines);

% mem = zeros(1,Nframes);
mem = [];
i = 1;

% Load mem_analysis.csv into matrix
line = fgetl(fid);
while(ischar(line))
    entries = split(line,';');
    for j = 1:length(entries)
        a = split(entries(j),',');
        if(length(a)==2)
            name = a{1};
            num = str2double(a{2});
            I = find(strcmp(names,name));
            if isempty(I)
                names{end+1} = name;
                I = length(names);
            end
            mem(I,i) = num;
        end
    end
    i = i + 1;
    line = fgetl(fid);
end

% Reorder the matrix such that the fixed allocations are at the bottom:
% --> Find all rows which are constant (sum of abs diff is zero). The >0 is
% to limit the reordering to constant values only.
[~,indx] = sort(sum(abs(diff(mem,[],2)),2)>0);
mem = mem(indx,:);
names = names(indx);

bar(mem',1,'stacked')
legend(names,'location','eastoutside','interpreter','none')
xlabel('frame')
ylabel('memory size')