【问题标题】:(Re-) Generating a draggable line in Matlab GUI(重新)在 Matlab GUI 中生成可拖动的线
【发布时间】:2014-10-14 16:43:58
【问题描述】:

我目前正在 Matlab GUI 中构建一个功能,用户可以更改屏幕上显示的绘图(通过弹出菜单......这不是问题,仅供参考!)并移动垂直线用鼠标穿过绘图(x 数据从这条线的位置返回)。首次生成 GUI 时创建此鼠标交互线没有问题,但一旦用户从弹出菜单中选择不同的数据集,就无法“重新生成”用户交互线。

我在GUI的打开函数中使用以下代码建立可拖动线:

handles.yline1 = line([x_start x_start],[y_min,y_max],'ButtonDownFcn',@(hObject,eventdata)postprocessingtry1('startdrag1_Fcn',hObject,eventdata,guidata(hObject)));

地点:

function startdrag1_Fcn(hObject, eventdata, handles) 

set(handles.figure2,'WindowButtonMotionFcn',@(hObject,eventdata)postprocessingtry1('dragging1_Fcn',hObject,eventdata,guidata(hObject)));

...而“dragging1_Fcn”是返回x位置的函数。

一旦我尝试在弹出菜单回调函数中使用相同的“handles.yline1 = ...”声明,就会发生错误:

使用 handle.handle/set 时出错 对象无效或已删除。

后处理出错try1>dragging1_Fcn(第341行)

set(handles.yline1,'XData',pt.CurrentPoint(1,1)*[1 1]);

任何关于在选择和绘制新数据集(通过弹出菜单)后如何重新生成用户交互线的建议将不胜感激。现在想来,我觉得可能在弹出菜单回调函数中引用hObject和eventdata可能与问题有关……但我不确定!

感谢您的宝贵时间,科林沃尔多

【问题讨论】:

  • 你的回调函数声明对我来说是模糊的。为什么不简单地 handles.yline1 = line(...,'ButtonDownFcn',@startdrag1_Fcn); ?每次定义回调函数时都有理由调用postprocessingtry1吗?
  • 同意@Hoki。否则,作为一个想法,您可以使用 'findobj('Type','Line')' 来获取当前在绘图上的线。如果它是空的,那么创建另一个?我现在无法对其进行测试,但这可能是一个开始。

标签: matlab user-interface matlab-guide


【解决方案1】:

这是一个可以满足您的要求的函数,它是一个 100% 由代码构建的 gui - 希望您可以从中获得想法并将其转换为您想要的:

function demoVerticalLine
  % Create the variable which is required to know if the line move is active or not.
  mouseDown = false;
  % Create the first value for the xlimMode (used in callbacks)
  xLimMode = 'auto';
  % create a dialog, with the mouse actions set
  hFig = dialog ( 'windowstyle', 'normal', 'WindowButtonMotionFcn', @MouseMove, 'WindowButtonUpFcn', @MouseUp );
  % create an axes
  ax = axes ( 'parent', hFig, 'position', [0.1 0.2 0.8 0.7], 'nextplot', 'add' );
  % create a popup menu
  pop = uicontrol ( 'parent', hFig, 'style', 'popupmenu', 'string', { 'sin', 'cos' }, 'Callback', @UpdatePlot );  
  % some dummy data
  x = -pi:0.01:pi;
  % an initial X for my example
  vLineX = randi(length(x));
  % On first time through create a plot
  userPlot = []; % init a value -> this is used to clear the previous user data
  UpdatePlot();
  % get the y limits of the data to be plotted
  ylim = get ( ax, 'ylim' );
  % plot the vertical line
  hLine = plot ( ax, [x(vLineX) x(vLineX)], ylim, 'ButtonDownFcn', @MouseDown );
  % a callback for plotting the user data
  function UpdatePlot ( obj, event )
    % delete any user data which is already plotted
    delete ( userPlot );
    % plot the user data
    switch get ( pop, 'value' )
      case 1 % sin
        userPlot = plot ( ax, x, sin(x), 'r.-' );
      case 2 % cos
        userPlot = plot ( ax, x, cos(x), 'b.-' );
    end
  end
  % a function which is called whenever the mouse is moved
  function MouseMove ( obj, event )
    % only run this section if the user has clicked on the line
    if mouseDown
      % get the current point on the axes
      cp = get ( ax, 'CurrentPoint' );
      % update the xdata of the line handle.
      set ( hLine, 'XData', [cp(1,1) cp(1,1)] );
    end
  end
  % callback from the user clicking on the line
  function MouseDown ( obj, event )
    % get the current xlimmode
    xLimMode = get ( ax, 'xlimMode' );
    % setting this makes the xlimits stay the same (comment out and test)
    set ( ax, 'xlimMode', 'manual' );
    % set the mouse down flag
    mouseDown = true;
  end
  % on mouse up 
  function MouseUp ( obj, event )
    % reset the xlim mode once the moving stops
    set ( ax, 'xlimMode', xLimMode );
    % reset the mouse down flag.
    mouseDown = false;
  end
end

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2011-06-12
    • 1970-01-01
    相关资源
    最近更新 更多