function addplot(x,y,n) %addplot(x,y) %Adds a line to a plot each time it is called, %using a new color and linestyle for each new line. %(start by calling plot; then add new ones with addplot) % %addplot(x,y,n) uses n as the index for the plot--if n is incremented %each time, you get a new color each time. The main point of this is that %if you call it with n=1, it starts a new plot, rather than your %needing to call a different function (plot) for the first one. % %Because there is a cycle of seven colors and four line styles, there are 28 different %unique combinations of line styles and colors used before they start repeating. persistent index %variable that is saved to keep track of which color was used last. if isempty(index), index = 2;, end %First time it is called, use second color if nargin > 2, index = n; end % Use index if it is given colorindex=mod(index-1,7)+1; lineindex = mod(index-1,4)+1; colororder = [0 0 1.0000 0 0.5000 0 1.0000 0 0 0 0.500 0.500 0.6500 0 0.6500 0.5500 0.5500 0 0.2500 0.2500 0.2500]; linestyleorder = {'-','--',':','-.'}; if index > 1, %normal situation--want to turn on hold to add plot hold on plot(x,y,linestyleorder{lineindex},'color',colororder(colorindex,:) ) hold off else %first time--want hold off hold off plot(x,y,linestyleorder{lineindex},'color',colororder(colorindex,:) ) end index = index+1; %increment index for next time.