-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaddptext.m
54 lines (46 loc) · 1.35 KB
/
addptext.m
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
49
50
51
52
53
54
% Add nicely formatted p values to the current axes.
%
% INPUTS
% x,y: coordinates
% p: p values
% precision: number of digits precision (default 3)
% prefix: optional prefix to add to each (e.g., 'p=' - default '')
% [varargin]: any additional varargin are passed to text
%
% OUTPUTS
% T: text handles
% newy: new max y position
%
% [T,newy] = addptext(x,y,p,[precision],[doprefix],[textargs])
function [T,newy] = addptext(x,y,p,precision,doprefix,varargin)
if ieNotDefined('precision')
precision = 3;
end
if ~ieNotDefined('doprefix')
doprefix = true;
end
pstr = p2str(p,precision,doprefix);
sy = size(y);
sx = size(x);
if ~isequal(sx,sy)
% attempt to deal with y scalar case
assert(isscalar(y),'y must be same size as x or scalar');
y = repmat(y,sx);
end
T = NaN(sx);
notnan = ~isnan(x);
% weird bug - text fails with single class inputs
T(notnan) = text(double(x(notnan)),double(y(notnan)),pstr(notnan),...
varargin{:});
set(T(p(notnan)<0.05),'fontangle','normal','fontweight','bold');
set(T(p(notnan)>0.05),'fontangle','italic','fontweight','normal');
newy = NaN;
if nargout<2 || ~any(notnan)
return
end
% if we made it here we need to work out y max
allpos = get(T(notnan),'extent');
allpos = cat(1,allpos{:});
% convert width/height to endpoint coordinate
allpos(:,3:4) = allpos(:,3:4) + allpos(:,1:2);
newy = max([allpos(:,2); allpos(:,4)]);