(重新)在Matlab GUI中生成可拖动的行

时间:2014-10-14 16:43:59

标签: matlab user-interface matlab-guide

我目前正在Matlab GUI中构建一个功能,用户可以更改屏幕上显示的图表(通过弹出菜单....这不是问题FYI!)并移动垂直线用鼠标在图表上(从该行的位置返回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位置的函数。

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

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

后处理1中的错误> dragging1_Fcn(第341行)

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

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

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

1 个答案:

答案 0 :(得分:0)

这是一个功能,可以满足您的要求,它是一个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