Hi , please how to make a loop for subplot , I need to plot plot 5 signals on 3 rows & 2 columns , signals to plot x , y , cr_x , cr_y and cr_xy given below in the code for auto-correlation and cross-correlation

조회 수: 2 (최근 30일)
% Autocorrealtion and cross correlation
f01=50;
f02=500;
fs=3000;
dt=1/fs;
t=0:dt:0.2;
L=length(t);
x=sin(2*pi*f01.*t)+(3+0.5*randn(1,L));
y=cos(2*pi*f02.*t+exp(-t/(randn(1,L))));
[cr_x,lgs_x] = xcorr(x); % Auto correlation of x
[cr_y,lgs_y] = xcorr(y); % Auto correlation of y
[cr_xy,lgs_xy] = xcorr(x,y); % Cross correlation of x and y
plot(t,x)
plot(lgs_x/fs,cr_x/L)
xlabel('Lag (x)')
plot(lgs_y/fs,cr_y/L)
plot(t,y)
xlabel('Lag (y)')
plot(lgs_xy/fs,cr_xy/L)
xlabel('Lag (x&y)')
grid on
grid minor

채택된 답변

Star Strider
Star Strider 2019년 2월 6일
I doubt that a loop would be more efficient than simply allocating the subplot calls using your existing code.
Try this:
figure
subplot(3,2,1)
plot(t,x)
grid on
grid minor
subplot(3,2,2)
plot(lgs_x/fs,cr_x/L)
xlabel('Lag (x)')
grid on
grid minor
subplot(3,2,3)
plot(lgs_y/fs,cr_y/L)
grid on
grid minor
subplot(3,2,4)
plot(t,y)
xlabel('Lag (y)')
grid on
grid minor
subplot(3,2,5)
plot(lgs_xy/fs,cr_xy/L)
xlabel('Lag (x&y)')
grid on
grid minor
If you absolutely must use a loop for this, your code becomes significantly more complicated, because you have to assign the variables you are plotting to elements of a cell array or structure.
  댓글 수: 2
Mohamad
Mohamad 2019년 2월 6일
Dear Star , thanks a lot ,
I need the loop to avoid repeat editing subplot , grid minor , etc
Star Strider
Star Strider 2019년 2월 6일
I just copied and pasted in the code I posted. That is much easier than creating all the cell arrays you would otherwise need for a loop.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by