【问题标题】:Dragging an axes with a mouse within a figure: Matlab Gui Guide在图中用鼠标拖动轴:Matlab Gui Guide
【发布时间】:2017-02-27 21:52:09
【问题描述】:

该类的目的是允许用户单击图像(即轴)并将其拖过图形。当检测到鼠标点击时,该函数检测鼠标位置位于哪个轴内并启动调用函数“movit”的“windowbuttonmotionfcn”。函数“movit”的目的是在鼠标按下时拖动鼠标移动选定的轴。

    function Mclicked(this, src, event)
        % get location of mouse click on the gui
         set(gca,'units','pix') ;
         mousePositionData = get(gca, 'CurrentPoint')
         this.x = mousePositionData(1,1);
         this.y = mousePositionData(1,2);

        %get origin position of all axes within the figure
        set(gca,'units','pix') ;
        AxesHandle=findobj(gcf,'Type','axes');
        pt1 = get(AxesHandle,{'Position','tightinset'}); % [left bottom right top] 

        %get the axes that mouse as clicked on and it in Values as a mat
            set(gcf,'windowbuttonmotionfcn',@( src, event) movit(this,src, event));
            set(gcf,'windowbuttonupfcn',@( src, event) stopmovit(this, src, event));   
        end
    end

我将鼠标第一次单击时的原始位置存储在变量 x 和 y 中。下面的算法在按钮按下时获取鼠标的新位置,并计算这两个鼠标移动之间的差异/距离。添加此差异以获得轴的新位置。

    function movit(this, src, event)
        %get location of new mouse position on the gui
        set(gca,'units','pix') ;
        mousePositionData = get(gca, 'CurrentPoint')
        this.x2 = mousePositionData(1,1);
        this.y2 = mousePositionData(1,2);
        this.distancex= this.x2-this.x;
        this.distancey= this.y2-this.y;
        %get the new location of the image.
        this.x2=this.Values(1,1)+this.distancex;
        this.y2=this.Values(1,2)+this.distancey;

        set(gca,'Position',[this.x2 this.y2 this.h this.w]); %x y h w
        drawnow;
    end

我遇到的问题是轴不会在鼠标附近移动。例如,当鼠标按钮按下并且鼠标向下甚至向上移动时,图像/轴向下移动并消失。它不会随鼠标光标移动。

我做了一个测试,以验证set(gca,'Position',[...]); %x y h w 是否正常工作,方法是使用我增加 1 的计数器在图形上移动并将值添加到原始位置。轴按预期移动并且对用户可见。因此,set(gca,'Position',[...]); %x y h w 工作正常。但是,我不确定错误是什么。我假设它与我想调用的计算或一段代码有关。

【问题讨论】:

  • 什么是this.Values
  • 用户点击的坐标轴的位置。它是一个垫子并存储 [x ,y, h, w]。
  • 这是从保存所有坐标轴位置的变量 pt1 获得的。
  • 我从您的问题中删除了模棱两可的 [guide] 标签,您已经拥有的 [matlab-guide] 是正确的。请至少阅读您将用于问题的标签的简短标签描述。
  • 谢谢。但是,仍然卡在这个问题上。

标签: matlab matlab-guide


【解决方案1】:

mousePositionData = get(gca, 'CurrentPoint') 行获取鼠标在坐标区中的坐标。但是,我们需要在图形或 gui 中获取鼠标位置。因此,我将 gca 改为 gcf。

另外,我发现 x 和 y 坐标需要互换。 set(gca,'Position',[this.y2 this.x2 this.h this.w]); %x y h w

图像现在鼠标光标旁边。仍然需要进行细微的调整来完善它。

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多