- 积分
- 30
- 注册时间
- 2004-11-17
- 仿真币
-
- 最后登录
- 1970-1-1
|
- function hf=mytabplot(flag,x,y)
- %mytabplot(flag,x,y)
- %
- % Author:WaitingForMe
- % Email:heroaq_2002@163.com
- % 请保留以上申明,谢谢
- %
- %说明:
- % 在数据线上添加游标显示数据的x,y轴的值
- % a或s键向左移动,w或者d键向又移动
- % 双击游标可以隐藏或者显示标签的值
- % 选中的游标边框为红色,未选中的边框为黑色
- % 右键单击可以读取两个标签的相对坐标值
- % 在单调上升或者下降区域标签的颜色为绿色,极大值和极小值点的颜色为黄色和兰色
- % x键删除当前标签,c删除所有标签
- %调用示例:
- % t=0:0.03:2.*pi;
- % mytabplot(0,t,sin(t));
- %
- switch flag
- case 0
- hold on;
- hl=line(x,y);
- set(get(gca,'xlabel'),'erasemode','xor');
- set(hl,'buttondownfcn','mytabplot(1)');
- case 1
- if strcmp(get(gcf,'selectiontype'),'normal')
- hl=gco;
- pointpos=get(gca,'currentPoint');
- xx=get(hl,'xdata');
- yy=get(hl,'ydata');
- ind=find(abs(xx-pointpos(1,1))==min(abs(xx-pointpos(1,1))));
- pos=[xx(ind),yy(ind)];
- hp=line(pos(1),pos(2),'marker','s','markersize',6,'LineWidth',2,'erasemode','xor');
- set(hp,'markeredgecolor','r')
- if pos(2)>yy(ind-1)&pos(2)>yy(ind+1)
- set(hp,'markerfacecolor','y')
- elseif pos(2)<yy(ind-1)&pos(2)<yy(ind+1)
- set(hp,'markerfacecolor','b')
- else
- set(hp,'markerfacecolor','g')
- end
- xlabelstr=['x = ' num2str(pos(1),'%8.4f') ' y = ' num2str(pos(2),'%8.4f')];
- textstr={['x = ' num2str(pos(1),'%8.4f')];['y = ' num2str(pos(2),'%8.4f')]};
- set(get(gca,'xlabel'),'string',xlabelstr);
- txpos=pos(1)+diff(get(gca,'xlim'))*0.01;
- typos=pos(2)+diff(get(gca,'ylim'))*0.01;
- ht=text(txpos,typos,textstr);
- set(ht,'VerticalAlignment','bottom','HorizontalAlignment','left','edgecolor',[0.6,0.6,0.6]);
- set(ht,'userdata',hp)
- set(hp,'userdata',[hl,ht,ind]);
- set(hp,'buttondownfcn','mytabplot(2)');
- set(gcf,'keypressFcn','mytabplot(3)');
- if length(get(gcf,'userdata'))~=0
- hlastp=get(gcf,'userdata');
- set(hlastp,'MarkerEdgeColor','k');
- set(gco,'MarkerEdgeColor','r');
- end
- set(gcf,'userdata',hp);
- end
- case 2
- st=get(gcf,'selectiontype');
- ud=get(gco,'userdata');
- if strcmp(st,'normal')
- hlastp=get(gcf,'userdata');
- try
- set(hlastp,'MarkerEdgeColor','k');
- catch
- %Author:WaitingForMe
- end
- set(gco,'MarkerEdgeColor','r')
- set(gcf,'userdata',gco);
- pos(1)=get(gco,'xdata');
- pos(2)=get(gco,'ydata');
- xlabelstr=['x = ' num2str(pos(1),'%8.4f') ' y = ' num2str(pos(2),'%8.4f')];
- set(get(gca,'xlabel'),'string',xlabelstr);
- elseif strcmp(st,'alt')
- try
- hlastp=get(gcf,'userdata');
- if gco==hlastp
- return;
- else
- xdif=abs(get(gco,'xdata')-get(hlastp,'xdata'));
- ydif=abs(get(gco,'ydata')-get(hlastp,'ydata'));
- xlabelstr=['Xdiff = ' num2str(xdif,'%8.4f') ' Ydiff = ' num2str(ydif,'%8.4f')];
- set(get(gca,'xlabel'),'string',xlabelstr);
- end
- catch
- %Author:WaitingForMe
- end
- elseif strcmp(st,'open')
- if strcmp(get(ud(2),'visible'),'off')
- set(ud(2),'visible','on');
- else
- set(ud(2),'visible','off');
- end
- end
- case 3
- hp=get(gcf,'userdata');
- ud=get(hp,'userdata');
- pressedkey=lower(get(gcf,'CurrentCharacter'));
- if double(pressedkey)==120
- try
- delete(ud(2));
- delete(hp);
- set(gcf,'userdata',[]);
- catch
- %Author:WaitingForMe
- end
- return
- end
- if double(pressedkey)==99
- ps=findobj('marker','s');
- try
- ts=get(ps,'userdata');
- temp=reshape([ts{:}],3,length(ts));
- temp=temp(2,;
- delete(temp);
- delete(ps);
- set(gcf,'userdata',[]);
- catch
- %Author:WaitingForMe
- end
- return
- end
- hl=ud(1);
- ht=ud(2);
- ind=ud(3);
- px=get(hp,'Xdata');
- xx=get(hl,'xdata');
- yy=get(hl,'ydata');
- if pressedkey=='a'|pressedkey=='s'
- if ind>1
- ind=ind-1;
- end
- elseif pressedkey=='w'|pressedkey=='d'
- if ind<length(xx)
- ind=ind+1;
- end
- end
- ud(3)=ind;
- pos=[xx(ind),yy(ind)];
- set(hp,'xdata',pos(1),'ydata',pos(2));
- if ind>1&ind<length(yy)
- if pos(2)>yy(ind-1)&pos(2)>yy(ind+1)
- set(hp,'markerfacecolor','y')
- elseif pos(2)<yy(ind-1)&pos(2)<yy(ind+1)
- set(hp,'markerfacecolor','b')
- else
- set(hp,'markerfacecolor','g')
- end
- end
- xlabelstr=['x = ' num2str(pos(1),'%8.4f') ' y = ' num2str(pos(2),'%8.4f')];
- textstr={['x = ' num2str(pos(1),'%8.4f')];['y = ' num2str(pos(2),'%8.4f')]};
- set(get(gca,'xlabel'),'String',xlabelstr);
- txpos=pos(1)+diff(get(gca,'xlim'))*0.01;
- typos=pos(2)+diff(get(gca,'ylim'))*0.01;
- set(ht,'position',[txpos,typos]);
- set(ht,'String',textstr);
- set(hp,'userdata',ud);
- otherwise
- %Author:WaitingForMe
- end
复制代码
[ 本帖最后由 bainhome 于 2007-9-15 22:47 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
评分
-
1
查看全部评分
-
|