Error: invalid or deleted object when using scatter in axes in GUI

Hello,
The following is a snippet of code from a function I am writing to create a GUI for visualizing data.
% Create axes for the scatterplot
theaxes2 = axes('Parent',thefigure, ...
'Units','Normalized', ...
'Position',[0.55 0.125 0.4 0.7]);
xlabel('PC 2');
ylabel('PC 3');
title('Principal Component Scatterplot');
% test that I can plot data on the theaxes2
scatter_handle = scatter(theaxes2, rand(10,1), rand(10,1));
Where I am using scatter just to test that I can throw data onto to the axes I just created. (This after it failing when I tried to plot real data further down the line)
That last line returns the error:
Error using handle.handle/set Invalid or deleted object.
This error occurs even if I replace theaxes2 with gca.
The error does NOT occur if I use line instead of scatter. (e.g. handle = line('parent', MYINFO.fileinfo{nfile}.theaxes2, 'xdata', 1:30, 'ydata', 1:30);)
But it does fail if I use "plot".
Here's the really weird thing. After the function has thrown the error and returned without completing the GUI, and I have the half-built GUI, if I paste that last line (using gca instead of the variable theaxes2) into the command line, it works fine.
I am tearing my hair out here. I have no idea why this won't work inside my function but will work if I create a figure from the command line and use the same function. How has the handle become invalid?
Thanks so much for your help.

 채택된 답변

Walter Roberson
Walter Roberson 2013년 4월 29일
An axes() call with properties does not make the new axes the current axes.
Try using
xlabel(theaxes2, 'PC 2');
ylabel(theaxes2, 'PC 3');
title(theaxes2, 'Principal Component Scatterplot');
scatter_handle = scatter(theaxes2, rand(10,1), rand(10,1));
However, it would be safer to do the scatter() first and then label and title.

댓글 수: 7

Thanks for the response. I didn't know that about axes not automatically making it the current axes, so that's useful.
But I'm still having trouble understanding why the following code (modified to take your advice) would run fine from the command like but break inside the function I have creating the GUI.
% Create axes for the scatterplot
theaxes2 = axes('Parent',thefigure, ...
'Units','Normalized', ...
'Position',[0.55 0.125 0.4 0.7]);
scatter_handle = scatter(theaxes2, rand(10,1), rand(10,1));
xlabel(theaxes2, 'PC 2');
ylabel(theaxes2, 'PC 3');
title(theaxes2, 'Principal Component Scatterplot');
Is there some figure property that could be causing this issue? It still breaks at the scatter() line and still says the problem is the invalid or deleted object.
You code runs fine. All I did was add this line:
thefigure = figure;
before your code. You must have some other code that you're not showing that is causing the problem. Set a breakpoint somewhere and step along until you see that theaxes2 goes from having a value to not having a value. Search your code for the word "clear" - do you see that anywhere?
I also have a question as to why the handle for the call to line() is MYINFO.fileinfo{nfile}.theaxes2 while the handle to the call to scatter() is just theaxes2. Can you explain that?
Running at the command line does the equivalent of drawnow() each time the command prompt would be shown.
@ Image Analyst:
There is no "clear" or reassigning of the variable. theaxes2 is assigned in the line you see; before then theaxes2 =[]; After then and until the very end of the code (well past the excerpt I have included), the value of the handle remains the same. I can use the handle once the GUI has been created to create a line on the axes in question using line('parent', theaxes2...) but if I try to use theaxes in the scatter() function it breaks.
I still don't understand why the line() function would work and not the scatter() function. Is there something I don't know about how scatter() is used for existing axes? I am fairly sure that I am calling it correctly, but if so then why does it break with scatter() and not with line()? I'd think that means the handle isn't the problem.
The MYINFO junk is just the way the variable is stored in the unabridged code--in this legacy code that I'm working with, many objects and values are stored in the global structure MYINFO. In different places, I have probably ten other GUIs that work using this convention and they're fine. I renamed the variable in my question for readability's sake after testing that it behaved the same regardless of whether the handle was stored in this struct or by itself.
line() always adds to the current plot; scatter clears the current plot if the internal equivalent of "hold" is not set on. (Sorry, I have a headache and some of the details are not coming to mind right now.)
That's fine -- it's helpful to know that they respond differently to the stuff that already exists within the axes. It inspired me to look at the figure properties.
When the main GUI figure was defined, the figure properties included IntegerHandle set to 'off'. When I removed this line so it was set to defauly and added 'hold on' to the end of the function creating the GUI suddenly I was able to plot my data.
I don't know why the integer handle property would have affected this (I copied the code defining the main figure from one of the package's other GUIs and I am not sure why my predecessor was using it) but it works now so I'm happy.
Thanks for all your help, guys.
Odd. Did you try "hold on" by itself ?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by