how to plot many plots at the same one?

1 view (last 30 days)
Hello,
I'm tying to show the result (five plots for detected patterns) of the code below in one plot but in vain,
any idea please
help is much appreciated
n = 50 ;
A = double( rand(n, n+1) > 0.5 ) ;
B = 2*A - 1 ;
patterns = {[1 0 1; 0 1 0; 1 0 1]; ... % X
[1 0 1; 0 1 0; 1 0 0]; ... % Y
[0 1 0; 1 0 1; 0 1 0]; ... % O
[0 0 0; 1 1 1; 0 0 0]; ... % -
[1 0 0; 1 0 0; 1 1 1]} ; % L
labels = {'X', 'Y', 'O', '-', 'L'} ;
matches = cell( size(labels )) ;
for pId = 1 : numel( patterns )
nel = numel( patterns{pId} ) ;
ker = 2*patterns{pId} - 1 ;
[r, c] = find( conv2(B, rot90(rot90(ker)), 'same') == nel ) ;
matches{pId} = [r, c] ;
figure(pId) ; clf ; hold on ;
spy(A) ;
plot( c, r, 'r+', 'MarkerSize', 20 ) ;
title( sprintf( 'Matches for "%s" pattern', labels{pId} )) ;
set( gcf, 'Units', 'normalized' ) ;
set( gcf, 'Position', [0 0 0.4 0.7] ) ;
end

Accepted Answer

Image Analyst
Image Analyst on 13 Jan 2014
Edited: Image Analyst on 13 Jan 2014
Try using subplot() and getting rid of the set()s:
n = 50;
A = double( rand(n, n+1) > 0.5 ) ;
B = 2*A - 1 ;
patterns = {[1 0 1; 0 1 0; 1 0 1]; ... % X
[1 0 1; 0 1 0; 1 0 0]; ... % Y
[0 1 0; 1 0 1; 0 1 0]; ... % O
[0 0 0; 1 1 1; 0 0 0]; ... % -
[1 0 0; 1 0 0; 1 1 1]} ; % L
labels = {'X', 'Y', 'O', '-', 'L'} ;
matches = cell( size(labels )) ;
for pId = 1 : numel( patterns )
subplot(2,3,pId); % Put plot into this location.
nel = numel( patterns{pId} ) ;
ker = 2*patterns{pId} - 1 ;
[r, c] = find( conv2(B, rot90(rot90(ker)), 'same') == nel ) ;
matches{pId} = [r, c] ;
% figure(pId) ; clf ;
hold on ;
spy(A) ;
plot( c, r, 'r+', 'MarkerSize', 20 ) ;
title( sprintf( 'Matches for "%s" pattern', labels{pId} )) ;
% set( gca, 'Units', 'normalized' ) ;
% set( gca, 'Position', [0 0 0.4 0.7] ) ;
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
  2 Comments
mika
mika on 15 Jan 2014
Thank you for your fast reply,
i'm trying to show the result of the shape recognition of 5 patterns at the same plot wich is only one plot. not the five plot at one using subplot.
any idea
thank you
Image Analyst
Image Analyst on 15 Jan 2014
Try this:
n = 50;
A = double( rand(n, n+1) > 0.5 ) ;
B = 2*A - 1 ;
patterns = {[1 0 1; 0 1 0; 1 0 1]; ... % X
[1 0 1; 0 1 0; 1 0 0]; ... % Y
[0 1 0; 1 0 1; 0 1 0]; ... % O
[0 0 0; 1 1 1; 0 0 0]; ... % -
[1 0 0; 1 0 0; 1 1 1]} ; % L
labels = {'X', 'Y', 'O', '-', 'L'} ;
for pId = 1 : numel( patterns )
nel = numel( patterns{pId} ) ;
ker = 2*patterns{pId} - 1 ;
[r, c] = find( conv2(B, rot90(rot90(ker)), 'same') == nel ) ;
% Append these r and c to the master list.
if pId == 1
% First one
allR = r;
allC = c;
else
allR = [allR; r(:)];
allC = [allC; c(:)];
end
end
plot(allC, allR, 'r+', 'MarkerSize', 20 ) ;
title( sprintf( 'Matches for "%s" pattern', labels{pId} )) ;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

Sign in to comment.

More Answers (1)

Bruno Pop-Stefanov
Bruno Pop-Stefanov on 15 Jan 2014
Hi Mika,
If you want to draw all the patterns in the same figure, you should indeed use hold on, however, by calling figure with a different ID number at each iteration of the loop, you create a new figure for each pattern. Also, you use clf which clears everything that was previously plotted on the current figure. Change your code to the following and that should work:
close all
clear all
clc
n = 50 ;
A = double( rand(n, n+1) > 0.5 ) ;
B = 2*A - 1 ;
patterns = {[1 0 1; 0 1 0; 1 0 1]; ... % X
[1 0 1; 0 1 0; 1 0 0]; ... % Y
[0 1 0; 1 0 1; 0 1 0]; ... % O
[0 0 0; 1 1 1; 0 0 0]; ... % -
[1 0 0; 1 0 0; 1 1 1]} ; % L
labels = {'X', 'Y', 'O', '-', 'L'} ;
matches = cell( size(labels )) ;
for pId = 1 : numel( patterns )
nel = numel( patterns{pId} ) ;
ker = 2*patterns{pId} - 1 ;
[r, c] = find( conv2(B, rot90(rot90(ker)), 'same') == nel ) ;
matches{pId} = [r, c] ;
figure(1) ; hold on ;
spy(A) ;
plot( c, r, 'r+', 'MarkerSize', 20 ) ;
title( sprintf( 'Matches for "%s" pattern', labels{pId} )) ;
set( gcf, 'Units', 'normalized' ) ;
set( gcf, 'Position', [0 0 0.4 0.7] ) ;
end

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!