MATLAB: How to edit and update a compass in the figure in MATLAB 7.6 (R2008a)

MATLAB

I have several axes in my figure. I want to include a compass on the upper right hand corner of the figure. I want the direction letters like 'N', 'E', 'S' and 'W' instead of the default degrees. Also I want to update the compass plot regularly in a loop.

Best Answer

The following example code demonstrates editing the compass plot. It also updates the compass.
function example
wdir= [45];
knots=[5] ;
rdir = wdir * pi/180;
%create the compass plot
f=figure;
[x,y] = pol2cart(rdir,knots);
ha=axes;
compass(ha,x,y);
%function to edit the axes and the plot
%pass figure and axes handles to which compass plot belongs
editplot(f,ha);
drawnow
%updating the compass plot
for k=1:360
wdir= [k];
knots=[5];
rdir = wdir * pi/180;
[x,y] = pol2cart(rdir,knots);
compass(ha,x,y); %pass the axes handle of the initial compass plot
editplot(f,ha);
drawnow
end
function editplot(f,ha)
set(0,'ShowHiddenHandles','on');
%setting the position of compass to top right corner of figure
set(ha,'Position',[0.75 0.75 0.20 0.20]);
%getting text handles
ht=findall(f,'Type','Text');
%finding the text that should be replaced by direction labels
ht1=findall(ht,'String','0');
ht2=findall(ht,'String','90');
ht3=findall(ht,'String','180');
ht4=findall(ht,'String','270');
set(ht,'String','');
%setting the direction labels
set(ht1,'String','E');
set(ht2,'String','N');
set(ht3,'String','W');
set(ht4,'String','S');