Objects getting deleted between user functions

1 次查看(过去 30 天)
I have a few user functions that I need to be interconnected; one is used to draw images and the others draw a set of moving cross hairs and a cursor over the images that are controlled by the user (with ButtonPressFcn etc.) Here's the section of code that applies...
% Here the cursor is created, calling user function 'startDragFcn'
handles.ccp = plot(handles.cpt(1,1),handles.cpt(1,2),'ro',...
'markerfacecolor','y',...
'ButtonDownFcn',@startDragFcn);
set(gcf ,'WindowButtonUpFcn',@stopDragFcn);
% Here's the startDragFcn user function
function startDragFcn(trial, eventdata, handles)
set(gcf,'WindowButtonMotionFcn', @draggingFcn)
guidata(hObject, handles);
function draggingFcn(hObject, eventdata, handles)
% Then there's some stuff about getting new info about the cross hairs position, no issues there
% Here we update the cross hairs position
set(gcf,'CurrentAxes',handles.axes1)
set(handles.ccp,'XData',handles.Hval,'Ydata',handles.Vval);
When it gets to this line, the following error message is displayed: "Error using matlab.graphics.chart.primitive.Line/get Invalid or deleted object." I have determined that the object hObject being rescued in startDragFcn is a line being drawn (which I'm pretty sure actually just refers to the cursor point), whereas the hObject in draggingFcn is a figure (so, the figure overall)? I've done a lot of reading about hObject and stuff, but I'm still confused about how that works, especially with referencing children/parent objects.
So my problem is that ccp is not being properly preserved in my code. Could that be due to the change in hObject? If so, what is happening and how do I address this problem? Thank you in advance

回答(1 个)

Adam
Adam 2016-6-17
You need this line after creating your handles.cpp
guidata( hObject, handles )
Unless you have just missed some code out in your post you don't appear to be doing that so the addition of cpp to the handles structure will be local to that function only and lost when the function goes out of scope.
  2 个评论
jmp448
jmp448 2016-6-20
Since both are contained in user functions, this doesn't accomplish anything does it? Since the startDragFcn is being called within the same line as the creation of handles.ccp, I thought that the code's course will run through this function up to this line, then skip to the startDragFcn, then after completing that move back to the original function (so that next command would only be executed after the startDragFcn stuff had already been completed)
Adam
Adam 2016-6-20
The line that create the handles.cpp is setting the ButtonDownFcn to be startDragFcn. That doesn't mean it will call it then.
It shouldn't call it until you actually press the button down after having set that, so the code should flow to the next line after this before startDragFcn is ever called.
This can easily be tested by just putting a disp statement on the line after and another one at the start of the startDragFcn to see when each gets triggered.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by