matlab画图,能够在图中时时显示鼠标在图中的位置(不需要点击!!!!),请问高手们怎么实现啊

有人跟我说过要用ginput函数和text函数配合使用,可是当我试了以后 发现只有当我鼠标点下后 嗯enter键才会在图上显示出来。我想实现那种我移动鼠标不用点击就能看到这个时候的左边:程序如下 请帮我修改
x=5:30;
y=x.^2-40.*x+400;
plot(x,y);
axis([5,30,-50,250]);
[a,b]=ginput;
s=[a,b];
text(15,200,num2str(s));
问过同样的问题 但是误点了采纳 作为工科生就应该一问到底哈 求高手帮忙解决

function tmouse(action)

global h
if nargin == 0, action = 'start'; end
switch(action)
    case 'start',
        x=5:30;
        y=x.^2-40.*x+400;
        plot(x,y);
        axis([5,30,-50,250]);
        title('Move your mouse !'); 
        set(gcf,'WindowButtonMotionFcn','tmouse move');
        h = text(2,-80,' ');
  case 'move',
        currPt = get(gca, 'CurrentPoint');
        x = currPt(1,1);
        y = currPt(1,2);
        set(h,'String',[num2str(x),',',num2str(y)]);
 end

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-24

新建个ff.m文件:

function ff
x = 5:30;
y = x.^2-40.*x+400;
plot(x,y);
axis([5,30,-50,250]);
 
tb = text;
set(gcf, 'WindowButtonMotionFcn', @callback);
 
function callback(hObject, event)
    loc = get(gca, 'CurrentPoint');
    loc = loc([1 3]);
    set(tb, 'string', num2str(loc), 'position', loc);
end
end

相似回答