找回密码
 注册
Simdroid-非首页
查看: 1154|回复: 13

一个画带游标的曲线的函数,辛苦了一个下午,与大家分享

[复制链接]
发表于 2004-11-20 20:50:06 | 显示全部楼层 |阅读模式 来自 北京东城
  1. function hf=mytabplot(flag,x,y)
  2. %mytabplot(flag,x,y)
  3. %
  4. %  Author:WaitingForMe
  5. %  Email:heroaq_2002@163.com
  6. %  请保留以上申明,谢谢
  7. %
  8. %说明:
  9. %   在数据线上添加游标显示数据的x,y轴的值
  10. %   a或s键向左移动,w或者d键向又移动
  11. %   双击游标可以隐藏或者显示标签的值
  12. %   选中的游标边框为红色,未选中的边框为黑色
  13. %   右键单击可以读取两个标签的相对坐标值
  14. %   在单调上升或者下降区域标签的颜色为绿色,极大值和极小值点的颜色为黄色和兰色
  15. %   x键删除当前标签,c删除所有标签
  16. %调用示例:
  17. %       t=0:0.03:2.*pi;
  18. %       mytabplot(0,t,sin(t));
  19. %
  20. switch flag
  21.     case 0
  22.         hold on;
  23.         hl=line(x,y);
  24.         set(get(gca,'xlabel'),'erasemode','xor');
  25.         set(hl,'buttondownfcn','mytabplot(1)');
  26.     case 1
  27.         if strcmp(get(gcf,'selectiontype'),'normal')
  28.             hl=gco;
  29.             pointpos=get(gca,'currentPoint');
  30.             xx=get(hl,'xdata');
  31.             yy=get(hl,'ydata');
  32.             ind=find(abs(xx-pointpos(1,1))==min(abs(xx-pointpos(1,1))));
  33.             pos=[xx(ind),yy(ind)];
  34.             hp=line(pos(1),pos(2),'marker','s','markersize',6,'LineWidth',2,'erasemode','xor');
  35.             set(hp,'markeredgecolor','r')
  36.             if pos(2)>yy(ind-1)&pos(2)>yy(ind+1)
  37.                 set(hp,'markerfacecolor','y')
  38.             elseif pos(2)<yy(ind-1)&pos(2)<yy(ind+1)
  39.                 set(hp,'markerfacecolor','b')
  40.             else
  41.                 set(hp,'markerfacecolor','g')
  42.             end
  43.             xlabelstr=['x = ' num2str(pos(1),'%8.4f') '  y = ' num2str(pos(2),'%8.4f')];
  44.             textstr={['x = ' num2str(pos(1),'%8.4f')];['y = ' num2str(pos(2),'%8.4f')]};
  45.             set(get(gca,'xlabel'),'string',xlabelstr);
  46.             txpos=pos(1)+diff(get(gca,'xlim'))*0.01;
  47.             typos=pos(2)+diff(get(gca,'ylim'))*0.01;
  48.             ht=text(txpos,typos,textstr);
  49.             set(ht,'VerticalAlignment','bottom','HorizontalAlignment','left','edgecolor',[0.6,0.6,0.6]);
  50.             set(ht,'userdata',hp)
  51.             set(hp,'userdata',[hl,ht,ind]);
  52.             set(hp,'buttondownfcn','mytabplot(2)');
  53.             set(gcf,'keypressFcn','mytabplot(3)');
  54.             if length(get(gcf,'userdata'))~=0
  55.                 hlastp=get(gcf,'userdata');
  56.                 set(hlastp,'MarkerEdgeColor','k');
  57.                 set(gco,'MarkerEdgeColor','r');
  58.             end
  59.             set(gcf,'userdata',hp);
  60.         end
  61.     case 2
  62.         st=get(gcf,'selectiontype');
  63.         ud=get(gco,'userdata');
  64.         if strcmp(st,'normal')
  65.             hlastp=get(gcf,'userdata');
  66.             try
  67.                 set(hlastp,'MarkerEdgeColor','k');
  68.             catch
  69.                 %Author:WaitingForMe
  70.             end
  71.             set(gco,'MarkerEdgeColor','r')
  72.             set(gcf,'userdata',gco);
  73.             pos(1)=get(gco,'xdata');
  74.             pos(2)=get(gco,'ydata');
  75.             xlabelstr=['x = ' num2str(pos(1),'%8.4f') '  y = ' num2str(pos(2),'%8.4f')];
  76.             set(get(gca,'xlabel'),'string',xlabelstr);
  77.         elseif strcmp(st,'alt')
  78.             try
  79.                 hlastp=get(gcf,'userdata');
  80.                 if gco==hlastp
  81.                     return;
  82.                 else
  83.                    xdif=abs(get(gco,'xdata')-get(hlastp,'xdata'));
  84.                    ydif=abs(get(gco,'ydata')-get(hlastp,'ydata'));
  85.                    xlabelstr=['Xdiff = ' num2str(xdif,'%8.4f') '  Ydiff = ' num2str(ydif,'%8.4f')];
  86.                    set(get(gca,'xlabel'),'string',xlabelstr);
  87.                 end
  88.             catch
  89.                 %Author:WaitingForMe
  90.             end
  91.         elseif  strcmp(st,'open')
  92.             if strcmp(get(ud(2),'visible'),'off')
  93.                 set(ud(2),'visible','on');
  94.             else
  95.                 set(ud(2),'visible','off');
  96.             end
  97.         end
  98.     case 3
  99.         hp=get(gcf,'userdata');
  100.         ud=get(hp,'userdata');
  101.         pressedkey=lower(get(gcf,'CurrentCharacter'));
  102.         if double(pressedkey)==120
  103.             try
  104.                 delete(ud(2));
  105.                 delete(hp);
  106.                 set(gcf,'userdata',[]);
  107.             catch
  108.                 %Author:WaitingForMe
  109.             end
  110.             return
  111.         end
  112.         if double(pressedkey)==99
  113.             ps=findobj('marker','s');
  114.             try
  115.                 ts=get(ps,'userdata');
  116.                 temp=reshape([ts{:}],3,length(ts));
  117.                 temp=temp(2,;
  118.                 delete(temp);
  119.                 delete(ps);
  120.                 set(gcf,'userdata',[]);
  121.             catch
  122.                 %Author:WaitingForMe
  123.             end
  124.             return
  125.         end
  126.         hl=ud(1);
  127.         ht=ud(2);
  128.         ind=ud(3);
  129.         px=get(hp,'Xdata');
  130.         xx=get(hl,'xdata');
  131.         yy=get(hl,'ydata');
  132.         if pressedkey=='a'|pressedkey=='s'
  133.             if ind>1
  134.                 ind=ind-1;
  135.             end
  136.         elseif pressedkey=='w'|pressedkey=='d'
  137.             if ind<length(xx)
  138.                 ind=ind+1;
  139.             end
  140.         end
  141.         ud(3)=ind;
  142.         pos=[xx(ind),yy(ind)];
  143.         set(hp,'xdata',pos(1),'ydata',pos(2));
  144.         if ind>1&ind<length(yy)
  145.             if pos(2)>yy(ind-1)&pos(2)>yy(ind+1)
  146.                 set(hp,'markerfacecolor','y')
  147.             elseif pos(2)<yy(ind-1)&pos(2)<yy(ind+1)
  148.                 set(hp,'markerfacecolor','b')
  149.             else
  150.                 set(hp,'markerfacecolor','g')
  151.             end
  152.         end
  153.         xlabelstr=['x = ' num2str(pos(1),'%8.4f') '  y = ' num2str(pos(2),'%8.4f')];
  154.         textstr={['x = ' num2str(pos(1),'%8.4f')];['y = ' num2str(pos(2),'%8.4f')]};
  155.         set(get(gca,'xlabel'),'String',xlabelstr);
  156.         txpos=pos(1)+diff(get(gca,'xlim'))*0.01;
  157.         typos=pos(2)+diff(get(gca,'ylim'))*0.01;
  158.         set(ht,'position',[txpos,typos]);
  159.         set(ht,'String',textstr);
  160.         set(hp,'userdata',ud);
  161.     otherwise
  162.         %Author:WaitingForMe
  163. end
复制代码

[ 本帖最后由 bainhome 于 2007-9-15 22:47 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

发表于 2004-11-20 22:55:40 | 显示全部楼层 来自 浙江杭州

Re:一个画带游标的曲线的函数,辛苦了一个下午,与大家分享

Simdroid开发平台
很不错
欢迎多发帖!!
发表于 2004-11-21 01:16:42 | 显示全部楼层 来自 上海

Re:一个画带游标的曲线的函数,辛苦了一个下午,与大家分享

谢谢了,已经下载!
不过建议你将函数结构稍微梳理一下,可读性和维护都将更容易!
发表于 2004-11-21 19:32:44 | 显示全部楼层 来自 大连理工大学

Re:一个画带游标的曲线的函数,辛苦了一个下午,与大家分享

mytabplot(flag,x,y)
中的flag是啥意思?
好像取0 时才好使。
 楼主| 发表于 2004-11-21 22:44:31 | 显示全部楼层 来自 北京东城

Re:一个画带游标的曲线的函数,辛苦了一个下午,与大家分享

silt wrote:
mytabplot(flag,x,y)
中的flag是啥意思?
好像取0 时才好使。
这个flag是一个入口参数,决定不同的画图方式。仔细读读程序你就明白了哈,为了方便,我把这个程序写成了一个递归的程序了,flag为0时表示曲线不存在的时候画出曲线,在flag为其他参数时执行不同的操作。所以画图的时候,flag只取0:)
发表于 2004-11-22 10:53:01 | 显示全部楼层 来自 大连理工大学

Re:一个画带游标的曲线的函数,辛苦了一个下午,与大家分享

明白了,谢谢!
发表于 2007-9-15 14:25:48 | 显示全部楼层 来自 陕西西安

大哥,你的程序里的这一句是什么,我的显示怎么怪怪的
       temp=temp(2,:);
回复 不支持

使用道具 举报

发表于 2007-12-18 19:32:58 | 显示全部楼层 来自 浙江杭州
temp=temp(2,:);

":)" 被显示为笑脸
回复 不支持

使用道具 举报

发表于 2007-12-18 19:33:38 | 显示全部楼层 来自 浙江杭州
: )

取任意行、列
回复 不支持

使用道具 举报

发表于 2007-12-19 16:02:31 | 显示全部楼层 来自 湖北荆州
??? function hf=mytabplot(flag,x,y)
    |
Error: Function definitions are not permitted at the prompt or in scripts.
这样的提示是什么意思呢?怎么修改呢?
回复 不支持

使用道具 举报

发表于 2008-1-12 23:56:01 | 显示全部楼层 来自 四川成都
下载了,很好用。谢谢楼主共享!

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2008-2-3 11:14:43 | 显示全部楼层 来自 北京
第117行 应改为temp=temp( 2 , : );可能粘贴的时候没有选择完全。
回复 不支持

使用道具 举报

发表于 2008-4-23 15:33:13 | 显示全部楼层 来自 北京工业大学
xlabelstr=['x='num2str(pos(1),'%8.4f') 'y='num2str(pos(2),'%8.4f')];
??? Error: File: C:\MATLAB7\work\youbiao.m Line: 38 Column: 28
Missing MATLAB operator.实在找不出哪里错了!!:Q
回复 不支持

使用道具 举报

发表于 2008-4-24 11:27:55 | 显示全部楼层 来自 韩国
2004年的帖子你都顶上来了,你真是太有才了
首先我想告诉你,这个程序没有问题,堪称经典之作
如果你发现了问题,恭喜你,卸载旧版本,
重新装个更高级的版本好了。
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-5-23 13:00 , Processed in 0.086123 second(s), 18 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表