IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
plot_curve_pieces.m
Go to the documentation of this file.
1 %>@ingroup graphicsapi
2 %>@file
3 %>@brief Plots a curve in pieces if there is a discontinuity in the x-axis
4 %>
5 %> Mimics the plot() function, but does several plots if x does not have the same increment step.
6 %>
7 %> If the 'color' option is not passed in varargin, will rotate the colors
8 %> in the COLORS global
9 %
10 %> @param x vector of dimension [1]x[nf]
11 %> @param y Vector or matrix of dimension [nf]x[no]. The dimensions here
12 %> are inverted to keep consistent with the plot() function
13 %> @param varargin Will be by-passed to MATLAB's <code>plot()</code>
14 %> @return handles a cell of handles
15 function h = plot_curve_pieces(x, y, varargin)
16 
17 x(y == Inf) = [];
18 y(y == Inf) = [];
19 x = round(x*100)/100;
20 x = x(:); % Makes a column vector
21 sy = size(y);
22 if any(sy == 1)
23  y = y(:); % Makes a column vector
24  ny = 1; % number of data rows
25 else
26  ny = sy(2);
27 end;
28 
29 flag_rotate_color = 1;
30 for i = 1:numel(varargin)
31  if ischar(varargin{i})
32  if strcmp(upper(varargin{i}), 'COLOR')
33  flag_rotate_color = 0;
34  break;
35  end;
36  end;
37 end;
38 
39 args = varargin;
40 % modedelta = abs(mode(diff(x)));
41 % tol = 1.1*abs(modedelta); % distance difference tolerance
42 
43 no_points = length(x);
44 no_plots = 0;
45 for k = 1:ny
46  ynow = y(:, k)';
47  i = 1;
48  while 1
49  flag_break = i > no_points;
50  flag_plot = 0;
51  flag_new_piece = 0;
52 
53  if flag_break
54  flag_plot = 1;
55  else
56  if i == 1
57  flag_new_piece = 1;
58  else
59  if i > 2
60  dist = abs(x(i)-x(i-1));
61  tol = abs(x(i-1)-x(i-2));
62 
63  if dist > tol*1.9
64  flag_plot = 1;
65  flag_new_piece = 1;
66  end;
67  end;
68  end;
69  end;
70 
71  if flag_plot
72  no_plots = no_plots+1;
73 
74  if flag_rotate_color
75  args = [varargin, 'Color', find_color(k)];
76  else
77  args = varargin;
78  end;
79  if i-piece_start > 1
80  h{no_plots} = plot(x(piece_start:i-1), ynow(piece_start:i-1), args{:});
81  else
82  % 1-point plot
83  % h{no_plots} = plot(x(piece_start:i-1)*[1, 1]+[-modedelta, modedelta]*.3, ynow(piece_start:i-1)*[1, 1], varargin{:});
84  h{no_plots} = plot(x(piece_start), ynow(piece_start), varargin{:});
85  end;
86  hold on;
87  end;
88 
89  if flag_break
90  break;
91  end;
92 
93  if flag_new_piece
94  piece_start = i;
95  end;
96 
97  % if i > 1
98  % dist_old = dist;
99  % end;
100  i = i+1;
101  end;
102 end;
function find_color(in i)
function plot_curve_pieces(in x, in y, in varargin)