-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlims.m
More file actions
49 lines (42 loc) · 1.36 KB
/
lims.m
File metadata and controls
49 lines (42 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function [XLIM,YLIM,varargout] = lims(X,Y,varargin)
% Defines limits of a plot based on individual data
% Syntax is:
% [XLIM,YLIM,varargout] = lims(X,Y,varargin)
%
% One of the inputs can be empty. If so, its respective output is empty too.
% e.g. [~,YLIM] = lims( [] , randn(100,1) )
%
% Returns the limit values for X and Y
% varargout is an axis handle, if requested
%
% ---options are:
% 'parent' : parent axis handle (optional)
% if an axis handle is given, limits are
% automatically applied to it.
% 'margin' : size of the margin relative to range of the data
% format is [marginX , marginY]
% default 5% of data range, i.e. [0.05,0.05]
%
% O.Codol 4th May 2019
% codol.olivier@gmail.com
%---------------------------------------------------------
m = parsevarargin(varargin,'margin',[0.05,0.05]);
h = parsevarargin(varargin,'parent',0);
if ~isempty(X)
XLIM = [min(min(X)), max(max(X))];
XLIM = [XLIM(1)-diff(XLIM)*m(1) XLIM(2)+diff(XLIM)*m(1)];
else
XLIM = nan(1,2);
end
if ~isempty(Y)
YLIM = [min(min(Y)), max(max(Y))];
YLIM = [YLIM(1)-diff(YLIM)*m(2) YLIM(2)+diff(YLIM)*m(2)];
else
YLIM = nan(1,2);
end
if ~isclass('double',h)
if ~isempty(X); h.XLim = XLIM; end
if ~isempty(Y); h.YLim = YLIM; end
end
varargout = {h};
end