=========plot_to_center.m=====
function plot_to_center(x,y,xticks,yticks)
%by faruto@www.ilovematlab.cn QQ:516667408 Email:farutoliyang@gmail.com
%2009.7.11
hp = plot(x,y);
xl = xLim;
yl = yLim;
xt = get(gca,'XTick');
yt = get(gca,'YTick');
if nargin == 2
xticks = xt;
yticks = yt;
else
xt = xticks;
yt = yticks;
if xl(1)>min(xticks)
xl(1) = min(xticks);
end
if xl(2)<max(xticks)
xl(2) = max(xticks);
end
if yl(1)>min(yticks)
yl(1) = min(yticks);
end
if yl(2)<max(yticks)
yl(2) = max(yticks);
end
end
xep = 1/(xl(2)-xl(1));
yep = 1/(yl(2)-yl(1));
p = get(gca,'Position');
set(gca,'XTick',[],'XColor','w') ;
set(gca,'YTick',[],'YColor','w') ;
x = [xl(1) xl(2)];
y = [( yl(1)+yl(2) )/2 ( yl(1)+yl(2) )/2];
line(x,y,'Color',[1,0,0],'LineWidth',2);
annotation('arrow',[p(1)+p(3),p(1)+p(3)+0.02],[p(2)+p(4)/2,p(2)+p(4)/2],...
'Color',[1,0,0],'LineWidth',2);
y = [yl(1) yl(2)];
x = [( xl(1)+xl(2) )/2 ( xl(1)+xl(2) )/2];
line(x,y,'Color',[1,0,0],'LineWidth',2);
annotation('arrow',[p(1)+p(3)/2,p(1)+p(3)/2],[p(2)+p(4),p(2)+p(4)+0.02],...
'Color',[1,0,0],'LineWidth',2);
for i = 1:length(xticks)
x = [xticks(i),xticks(i)];
y = [( yl(1)+yl(2) )/2 ( yl(1)+yl(2) )/2+0.02*1/yep];
line(x,y,'Color',[1,0,0],'LineWidth',2);
text(xticks(i),( yl(1)+yl(2) )/2-0.04*1/yep,num2str(xticks(i)),'Fontsize',12,...
'Color',[1,0,0]);
end
for i = 1:length(yticks)
x = [( xl(1)+xl(2) )/2 ( xl(1)+xl(2) )/2+0.02*1/xep];
y = [yticks(i),yticks(i)];
line(x,y,'Color',[1,0,0],'LineWidth',2);
text(( xl(1)+xl(2) )/2-0.065*1/xep,yticks(i),num2str(yticks(i)),'Fontsize',12,...
'Color',[1,0,0]);
end
grid on
=======================
============================plot_to_anywhere.m============
function plot_to_anywhere(x,y,xticks,yticks)
%by faruto@www.ilovematlab.cn QQ:516667408 Email:farutoliyang@gmail.com
%2009.7.11
hp = plot(x,y);
[xyuan,yyuan] = ginput(1);
xl = xLim;
yl = yLim;
xt = get(gca,'XTick');
yt = get(gca,'YTick');
if nargin == 2
xticks = xt;
yticks = yt;
else
xt = xticks;
yt = yticks;
if xl(1)>min(xticks)
xl(1) = min(xticks);
end
if xl(2)<max(xticks)
xl(2) = max(xticks);
end
if yl(1)>min(yticks)
yl(1) = min(yticks);
end
if yl(2)<max(yticks)
yl(2) = max(yticks);
end
end
xep = 1/(xl(2)-xl(1));
yep = 1/(yl(2)-yl(1));
p = get(gca,'Position');
set(gca,'XTick',[],'XColor','w') ;
set(gca,'YTick',[],'YColor','w') ;
x = [xl(1) xl(2)];
y = [( yl(1)+yl(2) )/2+yyuan ( yl(1)+yl(2) )/2+yyuan];
line(x,y,'Color',[1,0,0],'LineWidth',2);
annotation('arrow',[p(1)+p(3),p(1)+p(3)+0.02],[p(2)+p(4)/2+yyuan*yep*p(4),p(2)+p(4)/2+yyuan*yep*p(4)],...
'Color',[1,0,0],'LineWidth',2);
y = [yl(1) yl(2)];
x = [( xl(1)+xl(2) )/2+xyuan ( xl(1)+xl(2) )/2+xyuan];
line(x,y,'Color',[1,0,0],'LineWidth',2);
annotation('arrow',[p(1)+p(3)/2+xyuan*xep*p(3),p(1)+p(3)/2+xyuan*xep*p(3)],[p(2)+p(4),p(2)+p(4)+0.02],...
'Color',[1,0,0],'LineWidth',2);
for i = 1:length(xticks)
x = [xticks(i),xticks(i)];
y = [( yl(1)+yl(2) )/2+yyuan ( yl(1)+yl(2) )/2+yyuan+0.02*1/yep];
line(x,y,'Color',[1,0,0],'LineWidth',2);
text(xticks(i),( yl(1)+yl(2) )/2+yyuan-0.04*1/yep,num2str(xticks(i)),'Fontsize',12,...
'Color',[1,0,0]);
end
for i = 1:length(yticks)
x = [( xl(1)+xl(2) )/2+xyuan ( xl(1)+xl(2) )/2+xyuan+0.02*1/xep];
y = [yticks(i),yticks(i)];
line(x,y,'Color',[1,0,0],'LineWidth',2);
text(( xl(1)+xl(2) )/2+xyuan-0.065*1/xep,yticks(i),num2str(yticks(i)),'Fontsize',12,...
'Color',[1,0,0]);
end
grid on
==========================================================