V_TEXTHVC - write text on graph with specified alignment and colour Usage: (1) v_texthvc(x,y,'Hello','mlr') % align centre-baseline colour=red (2) v_texthvc(x,y,'\alpha','ml',[1 0 0]) % align centre-baseline colour=red (3) v_texthvc(x,y,'Hello','Mlr') % x position is normalized to (0,1) (4) v_texthvc([x k],y,'Hello','mlr') % add k*axis-width onto the x position Inputs: x x-position of text in graph coordinates (or normalized see below) alternatively x=[x0 k] positions at x0 + k*axis-width y y-position of text in graph coordinates (or normalized see below) alternatively y=[y0 k] positions at y0 + k*axis-height t text string to write on graph. Use \alpha for greek or x_1 for subscript p 3-character text string, 'hvc' specifying: horizontal reference point, h: l=left, c or m=middle, r=right vertical reference point, v: t=top, p=cap, c or m=middle, l=baseline, b=bottom colour, c: rgbcmykw q alternative colour specification as [r g b] each in range 0 to 1 If the horizontal or vertical reference point is given as a capital letter, the corresponding position is normalized to the axis range and should be in the range 0 to 1.
0001 function h=v_texthvc(x,y,t,p,q,r) 0002 %V_TEXTHVC - write text on graph with specified alignment and colour 0003 % 0004 %Usage: (1) v_texthvc(x,y,'Hello','mlr') % align centre-baseline colour=red 0005 % (2) v_texthvc(x,y,'\alpha','ml',[1 0 0]) % align centre-baseline colour=red 0006 % (3) v_texthvc(x,y,'Hello','Mlr') % x position is normalized to (0,1) 0007 % (4) v_texthvc([x k],y,'Hello','mlr') % add k*axis-width onto the x position 0008 % 0009 % Inputs: x x-position of text in graph coordinates (or normalized see below) 0010 % alternatively x=[x0 k] positions at x0 + k*axis-width 0011 % y y-position of text in graph coordinates (or normalized see below) 0012 % alternatively y=[y0 k] positions at y0 + k*axis-height 0013 % t text string to write on graph. Use \alpha for greek or x_1 for subscript 0014 % p 3-character text string, 'hvc' specifying: 0015 % horizontal reference point, h: l=left, c or m=middle, r=right 0016 % vertical reference point, v: t=top, p=cap, c or m=middle, l=baseline, b=bottom 0017 % colour, c: rgbcmykw 0018 % q alternative colour specification as [r g b] each in range 0 to 1 0019 % 0020 % If the horizontal or vertical reference point is given as a capital 0021 % letter, the corresponding position is normalized to the axis range 0022 % and should be in the range 0 to 1. 0023 0024 % Copyright (C) Mike Brookes 2014 0025 % Version: $Id: v_texthvc.m 10865 2018-09-21 17:22:45Z dmb $ 0026 % 0027 % VOICEBOX is a MATLAB toolbox for speech processing. 0028 % Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html 0029 % 0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0031 % This program is free software; you can redistribute it and/or modify 0032 % it under the terms of the GNU General Public License as published by 0033 % the Free Software Foundation; either version 2 of the License, or 0034 % (at your option) any later version. 0035 % 0036 % This program is distributed in the hope that it will be useful, 0037 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0038 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0039 % GNU General Public License for more details. 0040 % 0041 % You can obtain a copy of the GNU General Public License from 0042 % http://www.gnu.org/copyleft/gpl.html or by writing to 0043 % Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. 0044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0045 0046 if nargin<3 0047 error('<3 arguments'); 0048 elseif nargin>6 0049 error('>6 arguments'); 0050 end 0051 0052 switch nargin 0053 case 3 0054 h=text(x,y,char(t)); 0055 case 6 0056 % for compatibility with old version 0057 h=text(x,y,char(t),'HorizontalAlignment',p,'VerticalAlignment',q,'Color',r); 0058 otherwise 0059 if nargin==5 0060 r=q; % color specification 0061 elseif nargin==4 0062 r=p(3:end); % colour is at end of spec string 0063 end 0064 ix=find(lower(p(1))=='lcmr',1); 0065 if isempty(ix) 0066 error('invalid horizontal spec'); 0067 end 0068 iy=find(lower(p(2))=='tpcmlb',1); 0069 if isempty(iy) 0070 error('invalid vertical spec'); 0071 end 0072 vx={'left', 'center', 'center', 'right'}; 0073 vy={'top', 'cap', 'middle', 'middle', 'baseline', 'bottom'}; 0074 if numel(x)>1 0075 if strcmpi(get(gca,'xscale'),'log') 0076 x=exp(log(x(1))+log(get(gca,'xlim'))*[-1; 1]*x(2)); 0077 else 0078 x=x(1)+get(gca,'xlim')*[-1; 1]*x(2); 0079 end 0080 else 0081 if p(1)==upper(p(1)) 0082 if strcmpi(get(gca,'xscale'),'log') 0083 x=exp(log(get(gca,'xlim'))*[1-x; x]); 0084 else 0085 x=get(gca,'xlim')*[1-x; x]; 0086 end 0087 end 0088 end 0089 if numel(y)>1 0090 if strcmpi(get(gca,'yscale'),'log') 0091 y=exp(log(y(1))+log(get(gca,'ylim'))*[-1; 1]*y(2)); 0092 else 0093 y=y(1)+get(gca,'ylim')*[-1; 1]*y(2); 0094 end 0095 else 0096 if p(2)==upper(p(2)) 0097 if strcmpi(get(gca,'yscale'),'log') 0098 y=exp(log(get(gca,'ylim'))*[1-y; y]); 0099 else 0100 y=get(gca,'ylim')*[1-y; y]; 0101 end 0102 end 0103 end 0104 h=text(x,y,char(t),'HorizontalAlignment',vx{ix},'VerticalAlignment',vy{iy},'Color',r); 0105 end