Subplot for for loop

3 ビュー (過去 30 日間)
Andre Gilchrist
Andre Gilchrist 2018 年 3 月 5 日
編集済み: M 2018 年 3 月 5 日
I just want to make a simple sub plot but i cannot get around the range and domain parameters. many thanks in advance
Ts=1/128;
Fs=1/Ts;
for N=[32 64 128 256 512 1024]
n=0:N-1;
x=sin(20*pi*n*Ts)+sin(11*pi*n*Ts)+cos(121*pi*n*Ts/2);
freqresol=Fs/N;
freq=(-Fs/2):freqresol:(Fs/2-freqresol);
XF=fftshift(fft(x));
figure
plot(freq,abs(XF)/N);
xlim([-20*pi 20*pi])
title(['N = ',num2str(N)])
suptitle('x=sin(20*pi*n*Ts)+sin(11*pi*n*Ts)+cos(121*pi*n*Ts/2)')
end
  1 件のコメント
Rik
Rik 2018 年 3 月 5 日
You are creating a new figure each loop iteration. Also, you are not using the subplot function to create smaler axes objects, so using suptitle is not necessary (nor is having it inside your loop).

サインインしてコメントする。

回答 (1 件)

M
M 2018 年 3 月 5 日
編集済み: M 2018 年 3 月 5 日
subplot(m,n,p) divides the current figure into an m-by-n grid and creates axes in the position specified by p.
So in your case you can use something like:
idx=1;
for N=[...]
...
subplot(3,2,idx)
plot(freq,abs(XF)/N);
xlim([-20*pi 20*pi])
title(['N = ',num2str(N)])
idx=idx+1;
end

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by