Home > cbassSouthFunctions > CJCThesisFunctions > patchline.m

patchline

PURPOSE ^

Plot lines as patches (efficiently)

SYNOPSIS ^

function p = patchline(xs,ys,varargin)

DESCRIPTION ^

 Plot lines as patches (efficiently)

 SYNTAX:
     patchline(xs,ys)
     patchline(xs,ys,zs,...)
     patchline(xs,ys,zs,'PropertyName',propertyvalue,...)
     p = patchline(...)

 PROPERTIES: 
     Accepts all parameter-values accepted by PATCH.
 
 DESCRIPTION:
     p = patchline(xs,ys,zs,'PropertyName',propertyvalue,...)
         Takes a vector of x-values (xs) and a same-sized
         vector of y-values (ys). z-values (zs) are
         supported, but optional; if specified, zs must
         occupy the third input position. Takes all P-V
         pairs supported by PATCH. Returns in p the handle
         to the resulting patch object.
         
 NOTES:
     Note that we are drawing 0-thickness patches here,
     represented only by their edges. FACE PROPERTIES WILL
     NOT NOTICEABLY AFFECT THESE OBJECTS! (Modify the
     properties of the edges instead.)

     LINUX (UNIX) USERS: One test-user found that this code
     worked well on his Windows machine, but crashed his
     Linux box. We traced the problem to an openGL issue;
     the problem can be fixed by calling 'opengl software'
     in your <http://www.mathworks.com/help/techdoc/ref/startup.html startup.m>.
     (That command is valid at startup, but not at runtime,
     on a unix machine.)

 EXAMPLES:
%% Example 1:

 n = 10;
 xs = rand(n,1);
 ys = rand(n,1);
 zs = rand(n,1)*3;
 plot3(xs,ys,zs,'r.')
 xlabel('x');ylabel('y');zlabel('z');
 p  = patchline(xs,ys,zs,'linestyle','--','edgecolor','g',...
     'linewidth',3,'edgealpha',0.2);

%% Example 2: (Note "hold on" not necessary here!)

 t = 0:pi/64:4*pi;
 p(1) = patchline(t,sin(t),'edgecolor','b','linewidth',2,'edgealpha',0.5);
 p(2) = patchline(t,cos(t),'edgecolor','r','linewidth',2,'edgealpha',0.5);
 l = legend('sine(t)','cosine(t)');
 tmp = sort(findobj(l,'type','patch'));
 for ii = 1:numel(tmp)
     set(tmp(ii),'facecolor',get(p(ii),'edgecolor'),'facealpha',get(p(ii),'edgealpha'),'edgecolor','none')
 end

%% Example 3 (requires Image Processing Toolbox):
%%   (NOTE that this is NOT the same as showing a transparent image on 
%%         of the existing image. (That functionality is
%%         available using showMaskAsOverlay or imoverlay).
%%         Instead, patchline plots transparent lines over
%%         the image.)

 img = imread('rice.png');
 imshow(img)
 img = imtophat(img,strel('disk',15));
 grains = im2bw(img,graythresh(img));
 grains = bwareaopen(grains,10);
 edges = edge(grains,'canny');
 boundaries = bwboundaries(edges,'noholes');
 cmap = jet(numel(boundaries));
 ind = randperm(numel(boundaries));
 for ii = 1:numel(boundaries)
 patchline(boundaries{ii}(:,2),boundaries{ii}(:,1),...
     'edgealpha',0.2,'edgecolor',cmap(ind(ii),:),'linewidth',3);
 end

 Written by Brett Shoelson, PhD
 brett.shoelson@mathworks.com
 5/31/2012
 
 Revisions:
 6/26 Improved rice.png example, modified FEX image.

 Copyright 2012 MathWorks, Inc.

 See also: patch, line, plot

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function p = patchline(xs,ys,varargin)
0002 % Plot lines as patches (efficiently)
0003 %
0004 % SYNTAX:
0005 %     patchline(xs,ys)
0006 %     patchline(xs,ys,zs,...)
0007 %     patchline(xs,ys,zs,'PropertyName',propertyvalue,...)
0008 %     p = patchline(...)
0009 %
0010 % PROPERTIES:
0011 %     Accepts all parameter-values accepted by PATCH.
0012 %
0013 % DESCRIPTION:
0014 %     p = patchline(xs,ys,zs,'PropertyName',propertyvalue,...)
0015 %         Takes a vector of x-values (xs) and a same-sized
0016 %         vector of y-values (ys). z-values (zs) are
0017 %         supported, but optional; if specified, zs must
0018 %         occupy the third input position. Takes all P-V
0019 %         pairs supported by PATCH. Returns in p the handle
0020 %         to the resulting patch object.
0021 %
0022 % NOTES:
0023 %     Note that we are drawing 0-thickness patches here,
0024 %     represented only by their edges. FACE PROPERTIES WILL
0025 %     NOT NOTICEABLY AFFECT THESE OBJECTS! (Modify the
0026 %     properties of the edges instead.)
0027 %
0028 %     LINUX (UNIX) USERS: One test-user found that this code
0029 %     worked well on his Windows machine, but crashed his
0030 %     Linux box. We traced the problem to an openGL issue;
0031 %     the problem can be fixed by calling 'opengl software'
0032 %     in your <http://www.mathworks.com/help/techdoc/ref/startup.html startup.m>.
0033 %     (That command is valid at startup, but not at runtime,
0034 %     on a unix machine.)
0035 %
0036 % EXAMPLES:
0037 %%% Example 1:
0038 %
0039 % n = 10;
0040 % xs = rand(n,1);
0041 % ys = rand(n,1);
0042 % zs = rand(n,1)*3;
0043 % plot3(xs,ys,zs,'r.')
0044 % xlabel('x');ylabel('y');zlabel('z');
0045 % p  = patchline(xs,ys,zs,'linestyle','--','edgecolor','g',...
0046 %     'linewidth',3,'edgealpha',0.2);
0047 %
0048 %%% Example 2: (Note "hold on" not necessary here!)
0049 %
0050 % t = 0:pi/64:4*pi;
0051 % p(1) = patchline(t,sin(t),'edgecolor','b','linewidth',2,'edgealpha',0.5);
0052 % p(2) = patchline(t,cos(t),'edgecolor','r','linewidth',2,'edgealpha',0.5);
0053 % l = legend('sine(t)','cosine(t)');
0054 % tmp = sort(findobj(l,'type','patch'));
0055 % for ii = 1:numel(tmp)
0056 %     set(tmp(ii),'facecolor',get(p(ii),'edgecolor'),'facealpha',get(p(ii),'edgealpha'),'edgecolor','none')
0057 % end
0058 %
0059 %%% Example 3 (requires Image Processing Toolbox):
0060 %%%   (NOTE that this is NOT the same as showing a transparent image on
0061 %%%         of the existing image. (That functionality is
0062 %%%         available using showMaskAsOverlay or imoverlay).
0063 %%%         Instead, patchline plots transparent lines over
0064 %%%         the image.)
0065 %
0066 % img = imread('rice.png');
0067 % imshow(img)
0068 % img = imtophat(img,strel('disk',15));
0069 % grains = im2bw(img,graythresh(img));
0070 % grains = bwareaopen(grains,10);
0071 % edges = edge(grains,'canny');
0072 % boundaries = bwboundaries(edges,'noholes');
0073 % cmap = jet(numel(boundaries));
0074 % ind = randperm(numel(boundaries));
0075 % for ii = 1:numel(boundaries)
0076 % patchline(boundaries{ii}(:,2),boundaries{ii}(:,1),...
0077 %     'edgealpha',0.2,'edgecolor',cmap(ind(ii),:),'linewidth',3);
0078 % end
0079 %
0080 % Written by Brett Shoelson, PhD
0081 % brett.shoelson@mathworks.com
0082 % 5/31/2012
0083 %
0084 % Revisions:
0085 % 6/26 Improved rice.png example, modified FEX image.
0086 %
0087 % Copyright 2012 MathWorks, Inc.
0088 %
0089 % See also: patch, line, plot
0090 
0091 [zs,PVs] = parseInputs(varargin{:});
0092 if rem(numel(PVs),2) ~= 0
0093     % Odd number of inputs!
0094     error('patchline: Parameter-Values must be entered in valid pairs')
0095 end
0096 
0097 % Facecolor = 'k' is (essentially) ignored here, but syntactically necessary
0098 if isempty(zs)
0099     p = patch([xs(:);NaN],[ys(:);NaN],'k');
0100 else
0101     p = patch([xs(:);NaN],[ys(:);NaN],[zs(:);NaN],'k');
0102 end
0103 
0104 % Apply PV pairs
0105 for ii = 1:2:numel(PVs)
0106     set(p,PVs{ii},PVs{ii+1})
0107 end
0108 if nargout == 0
0109     clear p
0110 end
0111 
0112 function [zs,PVs] = parseInputs(varargin)
0113 if isnumeric(varargin{1})
0114     zs = varargin{1};
0115     PVs = varargin(2:end);
0116 else
0117     PVs = varargin;
0118     zs = [];
0119 end

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005