# annotation in polarplot

Version 1.0.1 (2.64 KB) by
I searched to annotate the polar plot, which needs normalized figure units. For Cartesian, we have a nice solution. This is the best solutio
Updated 23 May 2024

clc; clear all;
% Dr Kaluri V Ranga Rao life senior member IEEE princeton kaluri@ieee.org
close all;
theta = linspace(0,360,180);
max = 1;
r = linspace(max,0.2,180);
s = 60; e = 20;
ra = [r(s) r(e)];
th = [theta(s) theta(e)];
h = polarplot(theta*pi/180,r,'.-r',th*pi/180,ra,'ob');
for k=1:180
[xaf,yaf] = polarDS2nfu([theta(k) theta(k)]*pi/180,[r(k)*0.5 r(k)]);
hT = annotation('arrow',xaf,yaf);
pause(0.5);
end
function varargout = polarDS2nfu(varargin)
% polarDS2nfu Convert data space polar units into normalized figure units.
%
% [Xf, Yf] = DS2NFU(X, Y) converts X,Y are theta,r polar coordinates from
% data space to normalized figure units, using the current axes. This is
% useful as input for ANNOTATION.
%
% POSf = DS2NFU(POS) converts 4-element position vector, POS from
% data space to normalized figure units, using the current axes. The
% position vector has the form [Xo Yo Width Height], as defined here:
%
% web(['jar:file:D:/Applications/MATLAB/R2006a/help/techdoc/' ...
% 'help.jar!/creating_plots/axes_pr4.html'], '-helpbrowser')
%
% [Xf, Yf] = DS2NFU(HAX, X, Y) converts X,Y coordinates from
% data space to normalized figure units, on specified axes HAX.
%
% POSf = DS2NFU(HAX, POS) converts 4-element position vector, POS from
% data space to normalized figure units, using the current axes.
%
% Ex.
% % Create some data
% t = 0:.1:4*pi;
% s = sin(t);
%
% % Add an annotation requiring (x,y) coordinate vectors
% plot(t,s);ylim([-1.2 1.2])
% xa = [1.6 2]*pi;
% ya = [0 0];
% [xaf,yaf] = ds2nfu(xa,ya);
% annotation('arrow',xaf,yaf)
%
% % Add an annotation requiring a position vector
% pose = [4*pi/2 .9 pi .2];
% posef = ds2nfu(pose);
% annotation('ellipse',posef)
%
% % Add annotations on a figure with multiple axes
% figure;
% hAx1 = subplot(211);
% plot(t,s);ylim([-1.2 1.2])
% hAx2 = subplot(212);
% plot(t,-s);ylim([-1.2 1.2])
% [xaf,yaf] = ds2nfu(hAx1,xa,ya);
% annotation('arrow',xaf,yaf)
% pose = [4*pi/2 -1.1 pi .2];
% posef = ds2nfu(hAx2,pose);
% annotation('ellipse',posef)
% Michelle Hirsch
% mhirsch@mathworks.com
% Copyright 2006-2014 The MathWorks, Inc
% updated for polar kaluri@ieee.org
%% Process inputs
error(nargchk(1, 3, nargin))
aspectRatio = 1.15;
XaspectRatio = 0.69;
% Determine if axes handle is specified
if length(varargin{1})== 1 && ishandle(varargin{1}) && strcmp(get(varargin{1},'type'),'axes')
hAx = varargin{1};
varargin = varargin(2:end);
else
hAx = gca;
end;
errmsg = ['Invalid input. Coordinates must be specified as 1 four-element \n' ...
'position vector or 2 equal length (x,y) vectors.'];
% Proceed with remaining inputs
if length(varargin)==1 % Must be 4 elt POS vector
pos = varargin{1};
if length(pos) ~=4,
error(errmsg);
end;
else
[th,r] = deal(varargin{:});
[x y] = pol2cart([th(1) th(2)],[r(1) r(2)]*aspectRatio);
x = x*XaspectRatio;
if length(x) ~= length(y)
error(errmsg)
end
end
%% Get limits
axun = get(hAx,'Units');
set(hAx,'Units','normalized');
axpos = get(hAx,'Position');
ax = axis(hAx);
axlim =[-ax(4) ax(4) -ax(4)*aspectRatio ax(4)*aspectRatio];
axwidth = diff(axlim(1:2));
axheight = diff(axlim(3:4));
%% Transform data
if exist('x','var')
varargout{1} = (x-axlim(1))*axpos(3)/axwidth + axpos(1);
varargout{2} = (y-axlim(3))*axpos(4)/axheight + axpos(2);
else
pos(1) = (pos(1)-axlim(1))/axwidth*axpos(3) + axpos(1);
pos(2) = (pos(2)-axlim(3))/axheight*axpos(4) + axpos(2);
pos(3) = pos(3)*axpos(3)/axwidth;
pos(4) = pos(4)*axpos(4)/axheight;
varargout{1} = pos;
end
%% Restore axes units
set(hAx,'Units',axun)

### Cite As

Kaluri (2024). annotation in polarplot (https://www.mathworks.com/matlabcentral/fileexchange/166371-annotation-in-polarplot), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2024a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.1

There was a typo while I made a cut and paste, and it been corrected

1.0.0