Home > FMAToolbox > Plot > PiecewiseLinearAxis.m

PiecewiseLinearAxis

PURPOSE ^

PiecewiseLinearAxis - Label axis using piecewise linear values.

SYNOPSIS ^

function a = PiecewiseLinearAxis(x,axis,varargin)

DESCRIPTION ^

PiecewiseLinearAxis - Label axis using piecewise linear values.

  USAGE

    a = PiecewiseLinearAxis(x,axis,<options>)

    x              axis values
    axis           optional axis: 'x' or 'y' (default = 'x')
    <options>      options for function <a href="matlab:help plot">plot</a>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function a = PiecewiseLinearAxis(x,axis,varargin)
0002 
0003 %PiecewiseLinearAxis - Label axis using piecewise linear values.
0004 %
0005 %  USAGE
0006 %
0007 %    a = PiecewiseLinearAxis(x,axis,<options>)
0008 %
0009 %    x              axis values
0010 %    axis           optional axis: 'x' or 'y' (default = 'x')
0011 %    <options>      options for function <a href="matlab:help plot">plot</a>
0012 
0013 % Copyright (C) 2015 by Michaƫl Zugaro
0014 %
0015 % This program is free software; you can redistribute it and/or modify
0016 % it under the terms of the GNU General Public License as published by
0017 % the Free Software Foundation; either version 3 of the License, or
0018 % (at your option) any later version.
0019 
0020 % Check number of parameters
0021 if nargin < 1,
0022   error('Incorrect number of parameters (type ''help <a href="matlab:help PiecewiseLinearAxis">PiecewiseLinearAxis</a>'' for details).');
0023 end
0024 
0025 % Check parameters
0026 if ~isdvector(x,'<'),
0027   error('Incorrect values (type ''help <a href="matlab:help PiecewiseLinearAxis">PiecewiseLinearAxis</a>'' for details).');
0028 end
0029 if nargin < 2,
0030     axis = 'x';
0031 else
0032     axis = lower(axis);
0033 end
0034 if ~isastring(axis,'x','y'),
0035     varargin = {axis,varargin{:}};
0036     axis = 'x';
0037 end
0038 if isempty(varargin)
0039     varargin = {'k:'};
0040 end
0041 
0042 % Find transitions (gaps) between linear pieces, and show them graphically
0043 dx = diff(x);
0044 binSize = median(dx);
0045 gaps = [find(dx>2*binSize)+1];
0046 a = gca;
0047 set(a,[axis 'lim'],[min(x)-binSize/2 max(x)+binSize/2]);
0048 if isempty(gaps), return; end
0049 
0050 % Because Matlab uses linear axes, we will need a reference vector X starting at x(1), ending at x(end),
0051 % and of the same length as x. This is how Matlab really views the values on the axis.
0052 X = linspace(x(1),x(end),length(x))';
0053 
0054 % Graphically indicate gap locations
0055 if strcmp(axis,'x'),
0056     PlotHVLines(X(gaps),varargin{:});
0057 else
0058     PlotHVLines(X(gaps),'h',varargin{:});
0059 end
0060 
0061 % We will ask Matlab to set optimal ticks for each linear piece
0062 % To this end, we create temporary, invisible axes of the same physical size (in the figure window)
0063 % as each piece, set its limits, and get tick information from there; we then transfer these back
0064 % to the original axis
0065 P = get(a,'position');
0066 gaps = [1;gaps;length(x)];
0067 ticks = [];
0068 tickLabels = [];
0069 for i = 2:length(gaps),
0070     % Determine location of start/stop of current piece, in Matlab 'coordinates' (i.e. using X)
0071     start = X(gaps(i-1));
0072     stop = X(gaps(i));
0073     % Create temporary, invisible axes, and set their physical width (in the figure window) so that
0074     % it matches that of current linear piece
0075     if strcmp(axis,'x'),
0076         factor = P(3)/(x(end)-x(1))
0077         p = [0 0.25 (stop-start)*factor 0.5];
0078     else
0079         factor = P(4)/(x(end)-x(1));
0080         p = [0.25 0 0.5 (stop-start)*factor];
0081     end
0082     tmp = axes('position',p,'visible','off');
0083     % Set limits as the beginning and end of current piece
0084     set(tmp,[axis 'lim'],[x(gaps(i-1)) x(gaps(i)-1)]);
0085     % Get locations of ticks computed by Matlab
0086     l = str2num(get(tmp,[axis 'ticklabel']));
0087     % Translate into original axis coordinates (X)
0088     t = interp1(x,X,l);
0089     % Store and move to next piece
0090     tickLabels = [tickLabels;l];
0091     ticks = [ticks;t];
0092     delete(tmp);
0093 end
0094 % Set tick locations and labels
0095 set(a,[axis 'tick'],ticks,[axis 'TickLabel'],tickLabels);

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