Why does this axes handle become invalid

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 Comments

Why do you assume that the problem is in the lower part of the code, when the 3rd line fails?
The third line doesn't fail. The exact same line of code in a different pushbutton is when it fails.

Sign in to comment.

Answers (2)

My guess (since the whole thing won't run) is:
subplot(2, 1, 1)
Draws a new axes and erases the old.

5 Comments

Only the 'replace' argument forces a re-creation.
And that's why without a full working example: I'm guessin'
This is the full code, in case it helps. I also attached a .mat file to load when the GUI first opens. Specifically, the error occurs during the callback for the nodePopUp popup menu. I'm trying to set the focus back to nodePlotAxes so I can clear the current 3 subplots and re-generate them for the new, user selected, node.
function inhomBetSimGUI
% inhomBetSimGUI is used to examine the results of the inhomogeneous beta
% simulations. The data can be viewed in two ways:
% 1. Time evolution of infected state probabilities for the whole network
% 2. State and transition probabilities for each node separately
clear all
close all
userData = struct;
% Create then hide the UI as it's being created
mainWindow = figure('Visible', 'off', 'units', 'normalized',...
'outerposition', [0 0 1 1], 'MenuBar', 'none', 'ToolBar', 'none');
tabBut = uipanel('units', 'normalized', 'position', [0 0.95 1 0.05],...
'parent', mainWindow, 'visible', 'off', 'backgroundcolor', [1 1 1]);
netTab = uicontrol('style', 'pushbutton', 'string', 'By Network', 'units',...
'normalized', 'position', [0 0 0.5 1], 'parent', tabBut, 'callback',...
{@byNet_pushbutton_Callback});
nodeTab = uicontrol('style', 'pushbutton', 'string', 'By Node', 'units',...
'normalized', 'position', [0.5 0 0.5 1], 'parent', tabBut, 'callback',...
{@byNode_pushbutton_Callback});
% Create initial GUI
initPanel = uipanel('units', 'normalized', 'position', ...
[0 0 1 1], 'parent', mainWindow, 'visible', 'off');
loadPanel = uipanel('units', 'normalized', 'position', ...
[0.25 0.25 0.5 0.5], 'parent', initPanel);
uicontrol('style', 'text', 'string', 'Select Data to Load', 'units',...
'normalized', 'parent', loadPanel, 'position', [0.1 0.85 0.8 0.1]);
fNameBox = uicontrol('style', 'edit', 'string', 'C:\', 'units',...
'normalized', 'parent', loadPanel, 'position', [0.1 0.65 0.6 0.1],...
'BackgroundColor', [1 1 1], 'HorizontalAlignment', 'left');
uicontrol('style', 'pushbutton', 'string', '...', 'units',...
'normalized','parent', loadPanel, 'position', [0.8 0.65 0.1 0.1],...
'callback', {@fname_browse_Callback});
uicontrol('style', 'pushbutton', 'string', 'Load File',...
'units', 'normalized', 'parent', loadPanel, 'position',...
[0.1 0.5 0.8 0.1], 'callback', {@fname_load_Callback});
% Create by network tab
netPanel = uipanel('units', 'normalized', 'position', ...
[0 0 1 0.95], 'parent', mainWindow, 'visible', 'off');
% Add the plot components
plotWindow = uipanel('units', 'normalized', 'position',...
[0.025 0.025 0.7 0.95], 'parent', netPanel);
plotAxes = axes('units', 'normalized', 'position',...
[0.025 0.025 0.95 0.95], 'parent', plotWindow);
% Add the control components
userControl = uipanel('units', 'normalized', 'position',...
[0.75 0.025 0.225 0.95], 'parent', netPanel);
% Create by node panel
nodePanel = uipanel('units', 'normalized', 'position', ...
[0 0 1 0.95], 'parent', mainWindow, 'visible', 'off');
% Add the plot components
nodePlotWindow = uipanel('units', 'normalized', 'position',...
[0.025 0.025 0.7 0.95], 'parent', nodePanel);
nodePlotAxes = axes('units', 'normalized', 'position',...
[0.025 0.025 0.95 0.95], 'parent', nodePlotWindow);
% Add the control components
nodeUserControl = uipanel('units', 'normalized', 'position',...
[0.75 0.025 0.225 0.95], 'parent', nodePanel);
uicontrol('style', 'text', 'string', 'Node', 'units',...
'normalized', 'parent', nodeUserControl, 'position', [0.1 0.85 0.8 0.1]);
nodePopUp = uicontrol('style', 'popupmenu', 'units', 'normalized',...
'position', [0.1 0.7 0.8 0.2], 'parent', nodeUserControl, 'string',...
'-1', 'Callback', {@node_menu_Callback});
% Make GUI visible
set(mainWindow, 'visible', 'on');
set(initPanel, 'Visible', 'on');
% Filename brows pushbutton callback. Determine file to open.
function fname_browse_Callback(~, ~)
set(fNameBox, 'string', uigetfile);
end
% Filename load pushbutton callback. Load file and open network tab with data displayed.
function fname_load_Callback(~, ~)
load(get(fNameBox, 'string'), 'result');
userData.result = result;
userData.size = sqrt(length(result.network.init));
nodeVals = cellfun(@num2str, num2cell(1:1:userData.size^2), 'UniformOutput', false);
userData.chosenNode = 1;
set(mainWindow, 'CurrentAxes', nodePlotAxes);
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')
set(nodePopUp, 'string', nodeVals, 'value', userData.chosenNode);
set(initPanel, 'visible', 'off');
set(netPanel, 'visible', 'on');
set(tabBut, 'visible', 'on');
set(netTab, 'backgroundcolor', [0.9412 0.9412 0.9412]);
set(nodeTab, 'backgroundcolor', [0.5 0.5 0.5]);
end
% Network tab pushbutton callback. Display network tab.
function byNet_pushbutton_Callback(~, ~)
if(~strcmp(get(netPanel, 'Visible'), 'on'))
set(nodePanel, 'Visible', 'off');
set(netPanel, 'Visible', 'on');
set(netTab, 'backgroundcolor', [0.9412 0.9412 0.9412]);
set(nodeTab, 'backgroundcolor', [0.5 0.5 0.5]);
end
end
% Node tab pushbutton callback. Display node tab.
function byNode_pushbutton_Callback(~, ~)
if(~strcmp(get(nodePanel, 'Visible'), 'on'))
set(netPanel, 'Visible', 'off');
set(nodePanel, 'Visible', 'on');
set(nodeTab, 'backgroundcolor', [0.9412 0.9412 0.9412]);
set(netTab, 'backgroundcolor', [0.5 0.5 0.5]);
end
end
% Node Pop-up menu callback. Read the pop-up menu Value property to
% determine which item is currently displayed.
function node_menu_Callback(~, ~)
userData.chosenNode = get(nodePopUp, 'value');
% Set current data to the selected data set.
set(mainWindow, 'CurrentAxes', nodePlotAxes);
delete(get(nodePlotAxes, 'children'));
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')
end
end
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.
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?

Sign in to comment.

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.

Products

Asked:

on 24 Apr 2015

Answered:

on 18 Oct 2017

Community Treasure Hunt

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

Start Hunting!