How can I display a Biograph object in a subplot? - matlab

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));

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:
Tested on R2018a.
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.

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:

Related

MATLAB - Update Plot with Matrix

I want to plot data, which is stored in an array. A contains three columns, each column represents a different data set. The following code works fine:
A = [0 0 0;
0 1 0];
h = plot(A)
However, a new line is appended to A and the plot shall be updated. I read that you can update plots with set and 'XData':
A = [0 0 0;
0 1 0;
1 2 0];
set(h,'XData',A)
This throws me an error: Error using set.
Value must be a column or row vector. Is there any way to refresh the data instead of a new plot? The following works just fine?
A = [0 0 0;
0 1 0;
1 2 0];
h = plot(A)
The initial code
A = [0 0 0;
0 1 0];
h = plot(A)
generates three line objects, one for each column of A (check that h has size 3×1). So you need to update each of those lines in a loop. Also, you need to update both the 'XData' and 'YData' properties:
for k = 1:numel(h)
set(h(k), 'XData', 1:size(A,1), 'YData', A(:,k))
end
You could use linkdata (https://mathworks.com/help/matlab/ref/linkdata.html):
A = [
0 0 0;
0 1 0
];
plot(A);
linkdata on;
A = [
0 0 0;
0 1 0;
1 2 0
];
Another approach deleting the plot and redrawing it immediately after:
h = plot(x,y);
% modify data...
delete(h);
h = plot(x,y);

How do I create a plot like this grid in MATLAB?

I have data like this
-1 -1 -1 1 0 0 1 1 -1 0 1 -1 0 1
where each element of the vector is one of a several states. In this case (which is arbitrary and obviously only an example), are -1 0 1. I'm trying to make a plot like this grid:
The closest I was able to get was with a combination of spy and various tweaks:
%% ARBITRARY example data
states = [-1 0 1];
data = [-1 -1 -1 1 0 0 1 1 -1 0 1 -1 0 1];
%% Approximate plot using sparse matrix and spy
T = size(data, 2);
num_states = size(states, 2);
g = zeros(num_states, T);
for idx = 1:T
jdx = find(data(idx) == states, 1, 'first');
g(jdx, idx) = 1;
end
g = sparse(g);
%% Tweak plot
obj = figure();
obj.Color = 'white';
spy(g, 30)
s = obj.Children(1);
s.XLim = [1 T];
s.YLim = [1 num_states];
s.XLabel.String = '';
s.XGrid = 'on';
s.YTick = 1:num_states;
s.YTickLabel = num2cell(states);
s.GridLineStyle = '-';
s.YGrid = 'on';
However, this is far from ideal, since a) it's not actually a shaded grid, and b) the ticks on the y-axis are in descending order, starting from the bottom, because this is how spy functions, among other problems. How do I make a plot like this? I'm using MATLAB 2015b on a Windows 7 64-bit machine.
You'll want to play around with colours and grids, but this should be sufficient to get started:
data = [-1 -1 -1 1 0 0 1 1 -1 0 1 -1 0 1];
states = flipud(unique(data)');
im = bsxfun(#eq,data,states);
image(im);
colormap([1 1 1;0 0 0]);
axis equal;
axis tight;
set(gca,'XTick',1:length(data));
grid minor
set(gca,'YTickLabel',states);
The above results in

How to change size of grid & granularity in meshc function

Show mask by meshc function
z = [0 0 0 0;0 1 1 0;0 0 0 0];
meshc(z)
Output is:
Desired output:
There is a lot of guessing on my side, and I guess you want something like this:
%// data
z = [0 0 0 0;0 1 1 0;0 0 0 0];
%// grid
[n,m] = size(z);
[x,y] = ndgrid(1:n,1:m);
%// finer grid
[xq, yq] = ndgrid(linspace(1,n,100),linspace(1,m,100));
%// interpolation
F = griddedInterpolant(x, y, z, 'cubic')
zq = F(xq, yq);
%// interpolated plot
figure(1)
meshc(xq,yq,zq)

How to address positions of marks from simple matrix data in the boxes between axis lines?

What is the simplest way to create the following plot in Matlab from the data of the matrix "positions" containing the value 1 for black marks and - 1 for white marks?
positions=[0 1 0 0 0 0; 0 -1 0 0 0 0; 0 1 0 0 0 0; 1 -1 1 1 -1 0]
There you go:
% //Data
positions = [0 1 0 0 0 0; 0 -1 0 0 0 0; 0 1 0 0 0 0; 1 -1 1 1 -1 0];
S = 50; %// circle size. Adjust manually
%// Preparation
[M N] = size(positions);
hold on
%// Plot filled circles
[ip jp] = find(flipud(positions)>0);
plot(jp-.5,ip-.5,'ko','markersize',S,'markerfacecolor','k')
%// Plot empty circles
[in jn] = find(flipud(positions)<0);
plot(jn-.5,in-.5,'ko','markersize',S)
%// Plot grid lines
plot([0 N],(1:M).'*[1 1],'k');
plot((1:N).'*[1 1],[0 M],'k');
%// Set tick labels
set(gca,'xtick',.5:N,'ytick',.5:M)
set(gca,'xticklabel',char((1:N).'+'A'-1),'yticklabel',char((1:M).'+'0'))
%// Set axis size and aspect
axis equal
axis([0 N 0 M])
set(gca,'ticklength',[0 0]) %// no visible ticks

Matlab+ Graph theory+ weight assignment

I have been trying to label the edges of an undirected graph, also, i am using this matgraph tool! I succeeded in making a graph, i just want to assign weights to it... Please help!!
Here is what i tried,
clear all;
close all;
clc;
g=graph;
for k=1:6
add(g,k,k+1)
add(g,1,4)
add(g,5,7)
end
ndraw(g);
x=rand(1,1);
y=rand(1,1)
A =[0 x 0 x 0 0 0;
x 0 x 0 0 0 0;
0 x 0 x 0 0 0;
x 0 x 0 x 0 0;
0 0 0 y 0 x x;
0 0 0 0 x 0 x;
0 0 0 0 x x 0]
If I understood you correctly you can add this code after the code you wrote:
% get line info from the figure
lineH = findobj(gca, 'type', 'line');
xData = cell2mat(get(lineH, 'xdata')); % get x-data
yData = cell2mat(get(lineH, 'ydata')); % get y-data
% if an edge is between (x1,y1)<->(x2,y2), place a label at
% the center of the line, i.e. (x1+x2)/2 (y1+y2)/2 etc
labelposx=mean(xData');
labelposy=mean(yData');
% generate some random weights vector
weights=randi(21,length(labelposx),1);
% plot the weights on top of the figure
text(labelposx,labelposy,mat2cell(weights), 'HorizontalAlignment','center',...
'BackgroundColor',[.7 .9 .7]);