(重新)在Matlab GUI中生成可拖动的行 [英] (Re-) Generating a draggable line in Matlab GUI

查看:324
本文介绍了(重新)在Matlab GUI中生成可拖动的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently building up a functionality in a Matlab GUI whereby the user can change the plot displayed on the screen (via a pop-up menu.... this isn't the issue FYI!) and move a vertical line across the plot with the mouse (the x-data is returned from the position of this line). I have no issues creating this mouse-interactive line when the GUI is first generated, but cannot "re-generate" the user-interactive line once the user selects a different dataset from the pop-up menu.

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

I establish the draggable line using the following code in the opening function of the 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位置的函数.

...and "dragging1_Fcn" is the function that returns the x-position.

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

The error occurs once I try to use the same "handles.yline1 = ..." declaration within the popupmenu callback function:

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

Error using handle.handle/set Invalid or deleted object.

postprocessingtry1> dragging1_Fcn中的错误(第341行)

Error in postprocessingtry1>dragging1_Fcn (line 341)

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

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

任何有关在选择和绘制新数据集后(通过弹出菜单)如何重新生成用户交互行的建议,将不胜感激.现在考虑一下,我认为也许在弹出菜单回调函数中引用hObject和eventdata可能与此问题有关……但是我不确定!

Any advice as to how I can regenerate the user-interactive line after selecting and plotting a new dataset (via the pop-up menu) would be IMMENSELY appreciated. Thinking about it now, I think maybe referencing hObject and eventdata within the pop-up menu callback function may have something to do with the issue... but I'm not sure!

谢谢您的时间,科林·沃尔多(Colin Waldo)

Thank you for your time, Colin Waldo

推荐答案

这里是一个功能,可以满足您的要求,它的gui是用代码100%构建的-希望您可以从中得到启发并将其转换为你想要什么:

Here is a function which does what you ask for, its a gui built 100% from code - hopefully you can get the idea from it and convert it to do what you want:

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

这篇关于(重新)在Matlab GUI中生成可拖动的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆