a b/Supporting Functions/kakearney-boundedline-pkg-8179f9a/singlepatch/singlepatch.m
1
function varargout = singlepatch(varargin)
2
%SINGLEPATCH Concatenate patches to be plotted as one
3
%
4
% [xp, yp, zp, ...] = singlepatch(x, y, z, ...)
5
%
6
% Concatenates uneven vectors of x and y coordinates by replicating the
7
% last point in each polygon.  This allows patches with different numbers
8
% of vertices to be plotted as one, which is often much, much more
9
% efficient than plotting lots of individual patches.  
10
%
11
% Input variables:
12
%
13
%   x:   cell array, with each cell holding a vector of coordinates
14
%        associates with a single patch.  The input variables must all be
15
%        of the same size, and usually will correspond to x, y, z, and c
16
%        data for the patches.
17
%
18
% Output variables:
19
%
20
%   xp:  m x n array of coordinates, where m is the maximum length of the
21
%        vectors in x and n is numel(x).  
22
23
% Copyright 2015 Kelly Kearney
24
25
if nargin ~= nargout
26
    error('Must supply the same number of input variables as output variables');
27
end
28
29
nv = nargin;
30
vars = varargin;
31
32
sz = cellfun(@size, vars{1}(:), 'uni', 0);
33
sz = cat(1, sz{:});
34
35
if all(sz(:,1) == 1)
36
    for ii = 1:nv
37
        vars{ii} = catuneven(1, NaN, vars{ii}{:})';
38
    end
39
%     x = catuneven(1, NaN, x{:})';
40
%     y = catuneven(1, NaN, y{:})';
41
elseif all(sz(:,2) == 1)
42
    for ii = 1:nv
43
        vars{ii} = catuneven(2, NaN, vars{ii}{:});
44
    end
45
%     x = catuneven(2, NaN, x{:});
46
%     y = catuneven(2, NaN, y{:});
47
else
48
    error('Inputs must be cell arrays of vectors');
49
end
50
51
[ii,jj] = find(isnan(vars{1}));
52
53
ind = accumarray(jj, ii, [size(vars{1},2) 1], @min); 
54
ij1 = [ii jj];
55
ij2 = [ind(jj)-1 jj];
56
idx1 = sub2ind(size(vars{1}), ij1(:,1), ij1(:,2));
57
idx2 = sub2ind(size(vars{1}), ij2(:,1), ij2(:,2));
58
59
for ii = 1:nv
60
    vars{ii}(idx1) = vars{ii}(idx2);
61
end
62
63
varargout = vars;
64
65