AppDesigner中ButtonDownFcn在UIaxe绘制曲线后失效的解决方案

一、问题描述

  在App Designer中绘制曲线时,常用UIAxes控件进行,但是在使用控件回调函数的ButtonDownFcn时,如果绘制了曲线,无法在已绘制了曲线的区域点击响应响应。

二、问题分析

  该问题的原因是,绘图前,ButtonDown响应的控件是UIAxes,所以鼠标点击这个控件的任何区域都可以响应;当绘制曲线后,出现的曲线会覆盖在该控件上面,因为点击曲线所在的区域时,鼠标相应的是曲线而不是UIAxes,所以UIAxes的ButtonDown无法被触发。

三、解决方案

有两种方式

  第一种方式是,编写当前曲线的ButtonDown函数,仿照其他的ButtonDown就可以;

  第二种方式是,将当前图形的句柄获取后,HitTest和PickableParts设置为不可;

  因为第二种很简单,所以写一下,大致思路是先在前面插入私有变量,设置hx;

 properties (Access = private)
        hx %绘制曲线的句柄变量
    end
  
  然后在回调函数里面输入下面代码
——————————
app.hx=plot(app.UIAxes,x,y,'-g');
app.hx.HitTest="off";
app.hx.PickableParts="none";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的实现方法: 1. 在 App Designer ,创建一个按钮和一个 UIAxes。 2. 在按钮的回调函数,读取图片并在 UIAxes 上显示。 3. 在 UIAxes 上创建一个 ButtonDownFcn 回调函数,用于响应鼠标点击事件。 4. 在回调函数,根据鼠标点击位置绘制点和线段。 以下是示例代码: ```matlab % --- Executes when the "Draw" button is clicked function drawButtonPushed(app, event) % Read image img = imread('your_image.jpg'); % Display image on UIAxes imshow(img, 'Parent', app.UIAxes); % Set up UIAxes properties axis(app.UIAxes, 'image'); set(app.UIAxes, 'XTick', [], 'YTick', []); % Add ButtonDownFcn callback function set(app.UIAxes, 'ButtonDownFcn', @(src, evt) drawLine(app, evt)); end % Callback function for UIAxes ButtonDownFcn function drawLine(app, evt) % Get current mouse position currentPoint = app.UIAxes.CurrentPoint(1,1:2); % If this is the first point, store it and return if isempty(app.previousPoint) app.previousPoint = currentPoint; return; end % Draw line segment from previous point to current point line(app.UIAxes, [app.previousPoint(1) currentPoint(1)], ... [app.previousPoint(2) currentPoint(2)], 'Color', 'r'); % Store current point as previous point for next line segment app.previousPoint = currentPoint; end ``` 在 App Designer ,需要添加一个名为 "previousPoint" 的属性,用于存储上一个鼠标点击位置。也可以添加其他属性和方法以实现更复杂的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值