How can I display a Biograph object in a subplot?

290 views Asked by At

I want to plot some biographs in some subplots. Please help me to do this task. There is my code:

cm = [0 1 1 0 0;1 0 0 1 1;1 0 0 0 0;0 0 0 0 1;1 0 1 0 0];
cm2 = [0 1 1 0 1;1 0 1 1 1;1 0 0 0 0;0 0 0 0 1;1 0 1 1 0];

figure(18);
subplot(2,1,1);
view(biograph(cm));
subplot(2,1,2);
view(biograph(cm2));
2

There are 2 answers

0
Sardar Usama On BEST ANSWER

Code:

h(1) = figure;
view(biograph(cm));
fig(1)=gcf;       ax1=gca;   % Figure and axes handles
c1 = get(fig(1), 'Children');
copyobj(c1,h(1));            % Copying the object to h(1)

h(2) = figure;
view(biograph(cm2));
fig(2)=gcf;       ax2=gca;   % Figure and axes handles
c2 = get(fig(2), 'Children'); 
copyobj(c2,h(2));            % Copying the object to h(2)

figure;
% Creating and getting handles of subplot axes
% Retrieving properties of children and copying to new parents and hiding the axes lines
s1 = subplot(1,2,1);    bg1 = get(ax1,'children');  copyobj(bg1,s1);    axis off;
s2 = subplot(1,2,2);    bg2 = get(ax2,'children');  copyobj(bg2,s2);    axis off;

close(fig);    close(h);    % Closing previously opened figures

Output:

output

0
Dev-iL On

Here's a modification of @SardarUsama's answer that should be suitable for newer MATLAB versions (where gcf no longer returns a handle to the biograph figure):

function hF = q41124642()
cm  = [0 1 1 0 0; 1 0 0 1 1; 1 0 0 0 0; 0 0 0 0 1; 1 0 1 0 0];
cm2 = [0 1 1 0 1; 1 0 1 1 1; 1 0 0 0 0; 0 0 0 0 1; 1 0 1 1 0];

% Look for biograph figures before and after ploting to know which figures to use
hBG{1} = findall( 0, 'Tag', 'BioGraphTool' ); % Before
view( biograph(cm) );
view( biograph(cm2) );
hBG{2} = findall( 0, 'Tag', 'BioGraphTool' ); % After
hBG = setdiff( hBG{2}, hBG{1} );              % (The difference contains the new figures)

% Create a new figure to hold the subplots:
hF = figure();

% Create and get handles of subplot axes:
s(1) = subplot(1,2,1); 
s(2) = subplot(1,2,2);

% Retrieve children and copy to new parents, hiding the axes 
IS_CHILD_AX = arrayfun( @(x)isa( x, 'matlab.graphics.axis.Axes' ), hBG(1).Children );
copyobj( hBG(1).Children(IS_CHILD_AX).Children, s(1) ); 
copyobj( hBG(2).Children(IS_CHILD_AX).Children, s(2) );
axis( s, 'off' );

% Close the biograph figures:
close(hBG); 

Notes:

  1. Tested on R2018a.
  2. I knew I should look for 'Tag','BioGraphTool' by following biograph.view into biograph.bggui until I found some identifying features of the created figure. Had this been unavailable, we'd simply ask findall for a list of figures, twice, applying the same diff logic.