# How to combine multiple plots using subplot?

18 views (last 30 days)
Hi,
I'm new in Matlab world and I try to combine multiple plot using subplot, but when I run it, out of 15 graphs, only 7 graphs appear to me and I can't figure out what I wrote wrong.
Here is the code:
% -For the sampled signal given by the relation (2), let Fs = 8kHz:
% a) Plot the initial sinusoidal signal. Let the frequency of
% the sinusoidal signal Fo = 300 Hz and the samples taken
% during 10 ms. The phase is arbitrary. Represent the discrete signal.
% b) To represent the discrete signal with plot.
% In this case the points are connected with lines, so that
% the appearance of the sinusoidal signal is obvious. Connecting points
% with lines is a form of signal reconstruction that transforms the
% discrete signal continuously. This is not the ideal form given by
% the sampling theorem, but in many situations it is satisfactory.
clear all
close all
%Punctele a si b
Fo = 300;
t = 0:0.0001:0.01;
xa = cos(2*pi*Fo*t);
subplot(411);
plot(t, xa); grid on
title('Semnal analogic')
Fs = 8000;
T = 1/Fs;
n = 0:T:0.1;
xs = cos(2*pi*Fo*n);
subplot(412);
stem(n,xs,'r'); grid on
title('Semnal discret')
subplot(413)
plot(n,xs); grid on
hold on
title('Semnal discret afisat cu plot') % c) To make several representations of the discrete signal,
% modifying Fo from 100 Hz to 475 Hz in 125 Hz steps.
% Using subplot for representation
%Fo = 100 Hz
Fo1 = 100;
Fs1 = 8000;
T1 = 1/Fs1;
n1 = 0:T1:0.1;
xs1 = cos(2*pi*Fo1*n1);
subplot(421);
plot(n1,xs1); grid on
title('Semnal discret la F_{o} = 100 Hz')
%Fo = 225Hz
Fo2 = 225;
Fs2 = 8000;
T2 = 1/Fs2;
n2 = 0:T2:0.1;
xs2 = cos(2*pi*Fo2*n2);
subplot(422);
plot(n2,xs2); grid on
title('Semnal discret la F_{o} = 225 Hz') %Fo = 350 Hz
Fo3 = 225;
Fs3 = 8000;
T3 = 1/Fs3;
n3 = 0:T3:0.1;
xs3 = cos(2*pi*Fo3*n3);
subplot(423)
plot(n3,xs3); grid on
title('Semnal discret afisat cu plot la F_{o} = 350Hz')
%Fo = 475 Hz
Fo4 = 475;
Fs4 = 8000;
T4 = 1/Fs4;
n4 = 0:T4:0.1;
xs4 = cos(2*pi*Fo4*n4);
subplot(424)
plot(n4,xs4); grid on
title('Semnal discret afisat cu plot la F_{o} = 475 Hz') % d) Repeat c) but vary the sinusoidal frequency from 7525
% to 7900 Hz in 125 Hz steps. Compare the phenomenon compared to point c).
%Fo = 7525 Hz
Fo5 = 7525;
Fs5 = 8000;
T5 = 1/Fs5;
n5 = 0:T5:0.1;
xs5 = cos(2*pi*Fo5*n5);
subplot(431);
plot(n5,xs5); grid on
title('Semnal discret la F_{o} = 7525 Hz') %Fo = 7650 Hz
Fo6 = 7650;
Fs6 = 8000;
T6 = 1/Fs6;
n6 = 0:T6:0.1;
xs6 = cos(2*pi*Fo6*n6);
subplot(432);
plot(n6,xs6); grid on
title('Semnal discret la F_{o} = 7650 Hz')
%Fo = 7775 Hz
Fo7 = 7775;
Fs7 = 8000;
T7 = 1/Fs7;
n7 = 0:T7:0.1;
xs7 = cos(2*pi*Fo7*n7);
subplot(433)
plot(n7,xs7); grid on
title('Semnal discret afisat cu plot la F_{o} = 7775 Hz') %Fo = 7900 Hz
Fo8 = 7900;
Fs8 = 8000;
T8 = 1/Fs8;
n8 = 0:T8:0.1;
xs8 = cos(2*pi*Fo8*n8);
subplot(434)
plot(n8,xs8); grid on
title('Semnal discret afisat cu plot la F_{o} = 7900 Hz') % e) Repeat c) but vary the sinusoidal frequency from 32100
% to 32475 Hz in 125 Hz steps. Compare the phenomenon compared to point c).
%Fo = 32100 Hz
Fo9 = 32100;
Fs9 = 8000;
T9 = 1/Fs9;
n9 = 0:T9:0.1;
xs9 = cos(2*pi*Fs9*n9);
subplot(441);
plot(n9,xs9); grid on
title('Semnal discret la F_{o} = 32100 Hz') %Fo = 32225 Hz
Fo10 = 32225;
Fs10 = 8000;
T10 = 1/Fs10;
n10 = 0:T10:0.1;
xs10 = cos(2*pi*Fo10*n10);
subplot(442);
plot(n10,xs10); grid on
title('Semnal discret la F_{o} = 7650 Hz') %Fo = 32350 Hz
Fo11 = 32350;
Fs11 = 8000;
T11 = 1/Fs11;
n11 = 0:T11:0.1;
xs11 = cos(2*pi*Fo11*n11);
subplot(443)
plot(n11,xs11); grid on
title('Semnal discret afisat cu plot la F_{o} = 32350 Hz')
%Fo = 32475 Hz
Fo12 = 32475;
Fs12 = 8000;
T12 = 1/Fs12;
n12 = 0:T12:0.1;
xs12 = cos(2*pi*Fo12*n12);
subplot(444)
plot(n12,xs12); grid on
title('Semnal discret afisat cu plot la F_{o} = 7900 Hz') I will be happy if somebody will explain what is wrong and what i need to modify.
I wish a good day!

Cris LaPierre on 19 Oct 2021
New to MATLAB = MATLAB Onramp (Ch 9 introduces plotting)
I think the issue is that you are inconsistent with your subplot numbering. The first 2 inputs should be the same for every call to subplot. The third number calls out a specific axes in the grid (it increases moving left to right, top to bottom)
Side comment - you do not need hold on here.
%Punctele a si b
Fo = 300;
t = 0:0.0001:0.01;
xa = cos(2*pi*Fo*t);
subplot(4,4,1);
plot(t, xa); grid on
title('Semnal analogic')
Fs = 8000;
T = 1/Fs;
n = 0:T:0.1;
xs = cos(2*pi*Fo*n);
subplot(4,4,5);
stem(n,xs,'r'); grid on
title('Semnal discret')
subplot(4,4,9)
plot(n,xs); grid on
hold on
title('Semnal discret afisat cu plot')
% c) To make several representations of the discrete signal,
% modifying Fo from 100 Hz to 475 Hz in 125 Hz steps.
% Using subplot for representation
%Fo = 100 Hz
Fo1 = 100;
Fs1 = 8000;
T1 = 1/Fs1;
n1 = 0:T1:0.1;
xs1 = cos(2*pi*Fo1*n1);
subplot(4,4,2);
plot(n1,xs1); grid on
title('Semnal discret la F_{o} = 100 Hz')
%Fo = 225Hz
Fo2 = 225;
Fs2 = 8000;
T2 = 1/Fs2;
n2 = 0:T2:0.1;
xs2 = cos(2*pi*Fo2*n2);
subplot(4,4,6);
plot(n2,xs2); grid on
title('Semnal discret la F_{o} = 225 Hz')
%Fo = 350 Hz
Fo3 = 225;
Fs3 = 8000;
T3 = 1/Fs3;
n3 = 0:T3:0.1;
xs3 = cos(2*pi*Fo3*n3);
subplot(4,4,10)
plot(n3,xs3); grid on
title('Semnal discret afisat cu plot la F_{o} = 350Hz')
%Fo = 475 Hz
Fo4 = 475;
Fs4 = 8000;
T4 = 1/Fs4;
n4 = 0:T4:0.1;
xs4 = cos(2*pi*Fo4*n4);
subplot(4,4,14)
plot(n4,xs4); grid on
title('Semnal discret afisat cu plot la F_{o} = 475 Hz')
% d) Repeat c) but vary the sinusoidal frequency from 7525
% to 7900 Hz in 125 Hz steps. Compare the phenomenon compared to point c).
%Fo = 7525 Hz
Fo5 = 7525;
Fs5 = 8000;
T5 = 1/Fs5;
n5 = 0:T5:0.1;
xs5 = cos(2*pi*Fo5*n5);
subplot(4,4,3);
plot(n5,xs5); grid on
title('Semnal discret la F_{o} = 7525 Hz')
%Fo = 7650 Hz
Fo6 = 7650;
Fs6 = 8000;
T6 = 1/Fs6;
n6 = 0:T6:0.1;
xs6 = cos(2*pi*Fo6*n6);
subplot(4,4,7);
plot(n6,xs6); grid on
title('Semnal discret la F_{o} = 7650 Hz')
%Fo = 7775 Hz
Fo7 = 7775;
Fs7 = 8000;
T7 = 1/Fs7;
n7 = 0:T7:0.1;
xs7 = cos(2*pi*Fo7*n7);
subplot(4,4,11)
plot(n7,xs7); grid on
title('Semnal discret afisat cu plot la F_{o} = 7775 Hz')
%Fo = 7900 Hz
Fo8 = 7900;
Fs8 = 8000;
T8 = 1/Fs8;
n8 = 0:T8:0.1;
xs8 = cos(2*pi*Fo8*n8);
subplot(4,4,15)
plot(n8,xs8); grid on
title('Semnal discret afisat cu plot la F_{o} = 7900 Hz')
% e) Repeat c) but vary the sinusoidal frequency from 32100
% to 32475 Hz in 125 Hz steps. Compare the phenomenon compared to point c).
%Fo = 32100 Hz
Fo9 = 32100;
Fs9 = 8000;
T9 = 1/Fs9;
n9 = 0:T9:0.1;
xs9 = cos(2*pi*Fs9*n9);
subplot(4,4,4);
plot(n9,xs9); grid on
title('Semnal discret la F_{o} = 32100 Hz')
%Fo = 32225 Hz
Fo10 = 32225;
Fs10 = 8000;
T10 = 1/Fs10;
n10 = 0:T10:0.1;
xs10 = cos(2*pi*Fo10*n10);
subplot(4,4,8);
plot(n10,xs10); grid on
title('Semnal discret la F_{o} = 7650 Hz')
%Fo = 32350 Hz
Fo11 = 32350;
Fs11 = 8000;
T11 = 1/Fs11;
n11 = 0:T11:0.1;
xs11 = cos(2*pi*Fo11*n11);
subplot(4,4,12)
plot(n11,xs11); grid on
title('Semnal discret afisat cu plot la F_{o} = 32350 Hz')
%Fo = 32475 Hz
Fo12 = 32475;
Fs12 = 8000;
T12 = 1/Fs12;
n12 = 0:T12:0.1;
xs12 = cos(2*pi*Fo12*n12);
subplot(4,4,16)
plot(n12,xs12); grid on
title('Semnal discret afisat cu plot la F_{o} = 7900 Hz') Cordas Andrei Cristian on 19 Oct 2021
Thanks you, I understood.

the cyclist on 19 Oct 2021
Prior subplots that would be overlapped by later plots are deleted. Try this simple on your local machine.
figure
subplot(3,1,2)
plot(rand(3))
title('Just one plot. No problem')
figure
subplot(3,1,2)
plot(rand(3))
title('1st plot')
subplot(7,1,1)
plot(rand(3))
title('2nd plot does not overlap 1st. No problem.')
figure
subplot(3,1,2)
plot(rand(3))
subplot(2,1,1)
plot(rand(3))
title('2nd plot overlaps 1st. 1st plot deleted.')