Reputation: 1
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.
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)));
Where:
function startdrag1_Fcn(hObject, eventdata, handles)
set(handles.figure2,'WindowButtonMotionFcn',@(hObject,eventdata)postprocessingtry1('dragging1_Fcn',hObject,eventdata,guidata(hObject)));
...and "dragging1_Fcn" is the function that returns the x-position.
The error occurs once I try to use the same "handles.yline1 = ..." declaration within the popupmenu callback function:
Error using handle.handle/set Invalid or deleted object.
Error in postprocessingtry1>dragging1_Fcn (line 341)
set(handles.yline1,'XData',pt.CurrentPoint(1,1)*[1 1]);
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!
Thank you for your time, Colin Waldo
Upvotes: 0
Views: 1442
Reputation: 5672
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
Upvotes: 0