Why does this axes handle become invalid

조회 수: 8 (최근 30일)
Nicholas Trunfio
Nicholas Trunfio 2015년 4월 24일
답변: Steven Lord 2017년 10월 18일
I'm given the error message "Invalid object handle" from the following line of code: set(mainWindow, 'CurrentAxes', nodePlotAxes). mainWindow is the figure window containing my GUI, and nodePlotAxes is one of two axes in the GUI. I've narrowed the problem down to the section of code at the bottom of the post. I'm pretty confident this is where the problem is because of the four disp(ishandle()) commands. Somewhere in between them the nodePlotAxes loses its status as a handle. Any help is much appreciated. Also, the GUI is created programatically, not using GUIDE, if that makes a difference.
disp(ishandle(mainWindow));
disp(ishandle(nodePlotAxes));
set(mainWindow, 'CurrentAxes', nodePlotAxes); % Error not here, same command in a pushbutton
subplot(2, 1, 1)
hold on
plot(userData.result.time, userData.result.state.infected(userData.chosenNode,...
:), '--r', 'LineWidth', 3)
plot(userData.result.time, userData.result.state.susceptible(userData.chosenNode,...
:), '-b', 'LineWidth', 3)
hold off
grid
axis([0 length(userData.result.time)-1 0 1])
xlabel('Time')
ylabel('State Probabilities')
title(sprintf('Temporal Evolution of State Probabilities for Node %d', userData.chosenNode))
legend('Infected', 'Susceptible')
subplot(2, 2, 3)
hold on
plot(userData.result.time, userData.result.trans.pSS(userData.chosenNode,...
:), '-b', 'LineWidth', 3)
plot(userData.result.time, userData.result.trans.pSI(userData.chosenNode,...
:), '-r', 'LineWidth', 3)
hold off
grid
axis([0 length(userData.result.time)-1 0 1])
xlabel('Time')
ylabel('Probability')
title('Susceptible Transition Probabilities as a Function of Time')
legend('P_S_S', 'P_S_I')
subplot(2, 2, 4)
hold on
plot(userData.result.time, userData.result.trans.pIS(userData.chosenNode,...
:), '--b', 'LineWidth', 3)
plot(userData.result.time, userData.result.trans.pII(userData.chosenNode,...
:), '--r', 'LineWidth', 3)
hold off
grid
axis([0 length(userData.result.time)-1 0 1])
xlabel('Time')
ylabel('Probability')
title('Infected Transition Probabilities as a Function of Time')
legend('P_I_S', 'P_I_I')
disp(ishandle(mainWindow));
disp(ishandle(nodePlotAxes));
  댓글 수: 2
Jan
Jan 2015년 4월 24일
Why do you assume that the problem is in the lower part of the code, when the 3rd line fails?
Nicholas Trunfio
Nicholas Trunfio 2015년 4월 24일
The third line doesn't fail. The exact same line of code in a different pushbutton is when it fails.

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

답변 (2개)

Sean de Wolski
Sean de Wolski 2015년 4월 24일
My guess (since the whole thing won't run) is:
subplot(2, 1, 1)
Draws a new axes and erases the old.
  댓글 수: 5
Nicholas Trunfio
Nicholas Trunfio 2015년 4월 24일
You are correct about subplot(2, 1, 1) being the problem. Putting the disp(ishandle()) commands around just that line indicates this is when nodePlotAxes is no longer a valid handle.
I'm not quite sure why, or what the workaround is, to prevent this from happening though.
makr9962
makr9962 2017년 10월 18일
I guess i have the exact same problem. The after the first subplot command
disp(ishandle())
is 0. After more than two years, has anyone a solution?

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


Steven Lord
Steven Lord 2017년 10월 18일
The subplot calls are in fact the cause of this behavior. Use axes instead of subplot. From the Tips section of the documentation for the subplot function:
"To overlay axes, use the axes command instead. The subplot function deletes existing axes that overlap new axes. For example, subplot('Position',[.35 .35 .3 .3]) deletes any underlying axes, but axes('Position',[.35 .35 .3 .3]) positions new axes in the middle of the figure without deleting underlying axes."
I added the emphasis on the key sentence in that quote.

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by