Home > FMAToolbox > Plot > PlotIntervals.m

PlotIntervals

PURPOSE ^

PlotIntervals - Plot vertical bars or rectangles to show interval limits.

SYNOPSIS ^

function PlotIntervals(intervals,varargin)

DESCRIPTION ^

PlotIntervals - Plot vertical bars or rectangles to show interval limits.

 Given a list of intervals [start stop], draw a green vertical bar at
 the beginning of each interval, and a red vertical bar at the end of
 each interval or a grey rectangle representing the interval.

  USAGE

    PlotIntervals(intervals,<options>)

    intervals      list of [start stop] intervals
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'style'       'bars' for colored vertical bars, or 'rectangles' for
                   background shaded rectangles (default)
     'direction'   'h' for horizontal, or 'v' for vertical (default)
     'color'       rectangle color ('rectangles' mode, default = grey)
     'alpha'       rectangle transparency ('rectangles' mode, default = 1)
    =========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function PlotIntervals(intervals,varargin)
0002 
0003 %PlotIntervals - Plot vertical bars or rectangles to show interval limits.
0004 %
0005 % Given a list of intervals [start stop], draw a green vertical bar at
0006 % the beginning of each interval, and a red vertical bar at the end of
0007 % each interval or a grey rectangle representing the interval.
0008 %
0009 %  USAGE
0010 %
0011 %    PlotIntervals(intervals,<options>)
0012 %
0013 %    intervals      list of [start stop] intervals
0014 %    <options>      optional list of property-value pairs (see table below)
0015 %
0016 %    =========================================================================
0017 %     Properties    Values
0018 %    -------------------------------------------------------------------------
0019 %     'style'       'bars' for colored vertical bars, or 'rectangles' for
0020 %                   background shaded rectangles (default)
0021 %     'direction'   'h' for horizontal, or 'v' for vertical (default)
0022 %     'color'       rectangle color ('rectangles' mode, default = grey)
0023 %     'alpha'       rectangle transparency ('rectangles' mode, default = 1)
0024 %    =========================================================================
0025 %
0026 
0027 % Copyright (C) 2008-2013 by Gabrielle Girardeau & Michaƫl Zugaro
0028 %
0029 % This program is free software; you can redistribute it and/or modify
0030 % it under the terms of the GNU General Public License as published by
0031 % the Free Software Foundation; either version 3 of the License, or
0032 % (at your option) any later version.
0033 
0034 % Default values
0035 color = [0.9 0.9 0.9];
0036 alphaValue = 1;
0037 style = 'rectangles';
0038 direction = 'v';
0039 
0040 if nargin < 1,
0041   error('Incorrect number of parameters (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).');
0042 end
0043 if size(intervals,2) ~= 2,
0044   error('Incorrect list of intervals (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).');
0045 end
0046 
0047 % Backward compatibility: previous versions used the syntax PlotIntervals(intervals,style,direction)
0048 parsed = false;
0049 if (nargin == 2 || nargin == 3) && isastring(lower(varargin{1}),'rectangles','bars'),
0050     style = lower(varargin{1});
0051     parsed = true;
0052 end
0053 if nargin == 3 && isastring(lower(varargin{2}),'h','v'),
0054     direction = lower(varargin{2});
0055     parsed = true;
0056 end
0057 
0058 % Parse parameter list
0059 if ~parsed,
0060     for i = 1:2:length(varargin),
0061         if ~ischar(varargin{i}),
0062             error(['Parameter ' num2str(i+1) ' is not a property (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).']);
0063         end
0064         switch(lower(varargin{i})),
0065             case 'style',
0066                 style = lower(varargin{i+1});
0067                 if ~isastring(style,'bars','rectangles'),
0068                     error('Incorrect value for property ''style'' (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).');
0069                 end
0070             case 'direction',
0071                 direction = lower(varargin{i+1});
0072                 if ~isastring(direction,'h','v'),
0073                     error('Incorrect value for property ''direction'' (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).');
0074                 end
0075             case 'color',
0076                 color = lower(varargin{i+1});
0077                 if ~isastring(color,'r','g','b','c','m','y','k','w') && ~isdvector(color,'#3','>=0','<=1'),
0078                     error('Incorrect value for property ''direction'' (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).');
0079                 end
0080             case 'alpha',
0081                 alphaValue = varargin{i+1};
0082                 if ~isdscalar(alphaValue,'>=0','<=1'),
0083                     error('Incorrect value for property ''alpha'' (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).');
0084                 end
0085             otherwise,
0086                 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help PlotIntervals">PlotIntervals</a>'' for details).']);
0087         end
0088     end
0089 end
0090 
0091 hold on;
0092 yLim = ylim;
0093 xLim = xlim;
0094 if strcmp(style,'bars'),
0095     for i = 1:size(intervals,1),
0096         if strcmp(direction,'v'),
0097             plot([intervals(i,1) intervals(i,1)],yLim,'Color',[0 0.75 0]);
0098             plot([intervals(i,2) intervals(i,2)],yLim,'Color',[0.9 0 0]);
0099         else
0100             plot(xLim,[intervals(i,1) intervals(i,1)],'Color',[0 0.75 0]);
0101             plot(xLim,[intervals(i,2) intervals(i,2)],'Color',[0.9 0 0]);
0102         end
0103     end
0104 else
0105     for i=1:size(intervals,1),
0106         if strcmp(direction,'v'),
0107             dx = intervals(i,2)-intervals(i,1);
0108             dy = yLim(2)-yLim(1);
0109             rec = patch(intervals(i,1)+[0 0 dx dx],yLim(1)+[0 dy dy 0],color,'LineStyle','none');
0110             alpha(rec,alphaValue);
0111         else
0112             dx = xLim(2)-xLim(1);
0113             dy = intervals(i,2)-intervals(i,1);
0114             rec = patch(xLim(1)+[0 0 dx dx],intervals(i,1)+[0 dy dy 0],color,'LineStyle','none');
0115             alpha(rec,alphaValue);
0116             alpha(rec,alphaValue);
0117         end
0118         uistack(rec,'bottom');
0119     end
0120 end
0121 
0122

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005