Contents

plot histograms for sequence of runs in color coding

arguments xvals, yvals are nsteps x nruns arrays, where each column is the x output (xvals) and the y output (yvals) of a run

function plot_histograms(xvals,yvals,varargin)
default={'lw',5,'fpsym','ko','fpcol','k',...
    'ax',[],'ay',[],'nb',20,'cmpart',1/3,'axfac',1.2,...
    'plotmean',true,'setax',true};
options=MySetOptions(default,varargin);
fdeco={'fontweight','bold','fontsize',16};
if ishold
    washold=true;
else
    washold=false;
end
nruns=size(yvals,2);
[ysort,yind]=sort(yvals);
yint=NaN(options.nb+1,nruns);
xint=yint;
nint=yint;
for i=1:nruns
    yint(:,i)=linspace(ysort(1,i),ysort(end,i),options.nb+1);
    [yu,ind]=unique(ysort(:,i));
    xu=xvals(yind(:,i),i);
    nint(:,i)=interp1(yu,ind,yint(:,i));
    xint(:,i)=interp1(yu,xu(ind),yint(:,i));
end
nbins=diff(nint,[],1);
nmx=max(nbins);
cmap=colormap(hot);
cmap=cmap(end:-1:max(1,floor(size(cmap,1)*options.cmpart)),:);
for i=1:nruns
    for k=1:options.nb
        iclr=mean(nbins(k,i));
        iclr=max(1,floor(iclr/nmx(i)*size(cmap,1)));
        y=yint(k+(0:1),i);
        x=xint(k+(0:1),i);
        plot(x,y,'-','linewidth',options.lw,'color',cmap(iclr,:));
        hold on
    end
end
if options.plotmean
    plot(mean(xvals,1),mean(yvals,1),options.fpsym,'markerfacecolor',options.fpcol,'markersize',6);
end
if isempty(options.ax)
    %xm=1/2*(min(mean(xvals,1))+max(mean(xvals,1)));
    %xw=1/2*(max(mean(xvals,1))-min(mean(xvals,1)));
    %options.ax=xm+options.axfac*xw*[-1,1];
    options.ax= prctile(xvals(:),[10,90]);
end
if isempty(options.ay)
    %ym=1/2*(min(mean(yvals,1))+max(mean(yvals,1)));
    %yw=1/2*(max(mean(yvals,1))-min(mean(yvals,1)));
    %options.ay=ym+options.axfac*yw*[-1,1];
    options.ay= prctile(yvals(:),[10,90]);
end
if options.setax
    axis([options.ax(:)',options.ay(:)']);
end
set(gca,fdeco{:})
if ~washold
    hold off
end
end

parses optional arguments of a function and assigns them as fields of

function [options,passed_on]=MySetOptions(defaults,userargs,pass_on)

structure options

unknown arguments are passed on into cell array passed_on if pass_on is present and non-empty or false, otherwise and error message is generated

passed_on={};
% wrap cell arguments to avoid generating multiple structs
if isstruct(defaults)
    options=defaults;
elseif iscell(defaults)
    for i=1:length(defaults)
        if iscell(defaults{i})
            defaults{i}={defaults{i}};
        end
    end
    options=struct(defaults{:});
else
    error('defaults not recognized\n');
end
if nargin<3 || isempty(pass_on)
    pass_on=false;
end
for i=1:2:length(userargs)
    if isfield(options,userargs{i})
        options.(userargs{i})=userargs{i+1};
    else
        if ~pass_on
            error('option ''%s'' not recognized\n',userargs{i});
        else
            passed_on={passed_on{:},userargs{i},userargs{i+1}};
        end
    end
end
Error using plot_histograms (line 15)
Not enough input arguments.
end