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
    评论
好的,以下是一个简单的MATLAB App Designer的代码,可以在UIAxes上用鼠标画点连线: ```matlab classdef MyApp < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure UIAxes matlab.ui.control.UIAxes end % Properties that are hidden from App Designer properties (Hidden) LineHandle matlab.graphics.chart.primitive.Line Points double Count double end % Callbacks that handle component events methods (Access = private) % Button down function: UIAxes function UIAxesButtonDown(app, event) % Get the current point currentPoint = app.UIAxes.CurrentPoint(1, 1:2); % Add the current point to the points array app.Points(app.Count, :) = currentPoint; % Update the count app.Count = app.Count + 1; % Update the line if app.Count > 1 app.LineHandle.XData = app.Points(:, 1); app.LineHandle.YData = app.Points(:, 2); end % Draw a circle at the current point plot(app.UIAxes, currentPoint(1), currentPoint(2), 'o', 'MarkerSize', 8, 'LineWidth', 1); end end % App initialization and construction methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure and configure properties app.UIFigure = uifigure('Name', 'My App'); app.UIFigure.Position = [100 100 640 480]; % Create UIAxes app.UIAxes = uiaxes(app.UIFigure); title(app.UIAxes, 'Click and drag to draw a line') xlabel(app.UIAxes, 'X') ylabel(app.UIAxes, 'Y') app.UIAxes.Position = [20 20 600 400]; hold(app.UIAxes, 'on'); app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true); % Create an empty line object for drawing points app.LineHandle = plot(app.UIAxes, nan, nan, 'LineWidth', 2, 'Color', 'black'); % Initialize the points array and count app.Points = zeros(100, 2); app.Count = 1; end end % App creation and deletion methods (Access = public) % Construct app function app = MyApp % Create UIFigure and components createComponents(app) end % Code that executes before app deletion function delete(app) % Delete UIFigure when app is deleted delete(app.UIFigure) end end end ``` 这个代码将创建一个简单的App Designer应用程序,它将在UIAxes上启用鼠标点击和拖动,以便用户可以绘制线条。该应用程序将维护一个点数组,每次用户单击或拖动鼠标时将当前点添加到该数组。应用程序还将维护一个计数器,以跟踪添加到数组的点数。 当用户单击或拖动鼠标时,UIAxesButtonDown回调将被调用。此回调将获取当前点的坐标,将其添加到Points数组,并在UIAxes上绘制一个圆形表示该点。如果Points数组有两个或更多点,回调将更新LineHandle对象的XData和YData属性,以便在UIAxes上绘制线条。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值