Search code examples
matlabplotmatlab-figuresubplot

Why does assigning an object to a subplot claim object is deleted?


I want to make 4 separate sub-figures each in a different position. my goal was to setup the figures first and following add objects to each figure with the goal to animate the objects later. Everything seems to work fine until I try to parent the objects to the sub-figure. the error doesn't occur when I add the objects right after building the sub-figure using the following code.

H1 = subplot('Position',[0.2,0.2,0.2,0.2]);
rectangle('Parent',H1,'Position',[10,20,20,20])

The error seems to rise when I try to callback to a figure built afterwards as seen in the code I am currently working on below.

screencolor = [0,0,0];
StimWindow = figure('MenuBar','none', ...                    % Build Window for stimulus
    'Color',screencolor);

figuresize = get(0,'ScreenSize'); 

set(StimWindow,'Position',figuresize);
set(0,'defaultaxesposition',[0 0 1 1])
Stimsubfigures{1} = subplot('Position',[0,0,1,1]);       % First subplot figure which spans the entire screen 
set(Stimsubfigures{1},'xLim',[0,100])
set(Stimsubfigures{1},'YLim',[0,100])
set(Stimsubfigures{1},'Visible','off')

% create subplots for stim system 3 plate setup
for aa = 2:4
    Stimsubfigures{aa} = subplot(...
                                'Position',[.01+aa*.21,.2,.2,.2], ...
                                'color','none');
    set(Stimsubfigures{aa},'xLim',[0,100])
    set(Stimsubfigures{aa},'YLim',[0,100])
    set(Stimsubfigures{aa},'Visible','off')
end

OrtDish = rectangle(...
    'Parent',Stimsubfigures{1},...
    'Position', [0,0,100,100],...                            
    'facecolor',screencolor,...
    'edgecolor',[.5,0,0],...
    'curvature',[1,1],...
    'LineWidth',3);

The error displayed states an object cannot be attached to a deleted handle. I will provide the exact error when I reach a computer.

This is weird as the handle wasn't deleted, it was stored in a cell matrix.


Solution

  • The problem comes from the overlapping subplot: you draw a full-scale subplot and then on top of it some new ones.

    As the Matlab doc says,

    If a SUBPLOT specification causes a new axes to overlap an existing axes, the existing axes is deleted - unless the position of the new and existing axes are identical.

    If you replace subplot by axes, you are good to go.