Rebuilding a user-interactive element when redrawing axes in GUI

1 visualización (últimos 30 días)
Hello All,
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 drop-down menu.
I establish the user-interactive 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.
Thank you for your time, Colin Waldo

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 14 de Oct. de 2014
Colin - without seeing more code, especially with respect to what the popup menu is doing, it is somewhat difficult to offer solutions. But since you say that ..plotting a new dataset (via the pop-up menu).. this seems to suggest that you are using (perhaps) plot to plot something new on the axes. For example, the following
x = -2*pi:0.0001:2*pi;
y = sin(x);
hCurve = plot(gca,x,y);
hLine = line([0 0],[-1 1],'Color','r');
draws a vertical red line in the middle of the sine wave. Now if I plot some new data (similar to your user selecting something from the popup menu), then
hCurve = plot(gca,x,y+1);
my red line disappears from the axes as the new curve is drawn. If I try to get the properties of the line object via
get(hLine)
I get the same error as you observed for set
Error using handle.handle/get
Invalid or deleted object.
Because I have drawn something new on the axes (the modified curve), then all or (almost) child elements of the axes are cleared. So hLine (or your, handles.line1) becomes invalid.
I think that you can do one of two things - either re-create the line object in your popup menu callback as
hFig = handles.figure1;
handles.yline1 = line([x_start x_start], [y_min,y_max],'ButtonDownFcn',...
@(hFig,eventdata)postprocessingtry1('startdrag1_Fcn', ...
hFig,eventdata,guidata(hFig)));
guidata(hObject,handles);
Note how the above uses the handle to the figure (assumes the figure is named figure1).
Or, you issue a hold on command for the axes, and then just reset the data on your axes. So in your OpeningFcn you would do something like
hold(handles.axes1,'on');
And then just reset the XData and YData* whenever you go to plot new data (again, without knowing exactly what you are doing, I can only guess). In my example, this would become
x = -2*pi:0.0001:2*pi;
y = sin(x);
hold(gca,'on');
hCurve = plot(gca,x,y);
hLine = line([0 0],[-1 1],'Color','r');
Then modify the current plot
set(hCurve,'XData',x,'YData',y+1);
set(hLine,'XData',[-2*pi -2*pi],'YData',[0 2]);
The above two lines redraws the curve data given the new YData, and then redraws the line to be at -2pi.

Más respuestas (0)

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by