several for loop plots
이전 댓글 표시
Hello all,
I am writing a code with several for loops and within each for loop I have several plots. However, my plots generated by these three for loops get overwritten. I would appreciate any help.
Here is the part of my code that I do the plots:
for i= 1:length(rts)
[IR,fs] = audioread(strjoin([inPath,allWavs(i)],'\'));
lIR = IR(:,1);
figure()
plot(t,rIR_SPL); xlabel('Seconds'); ylabel('Amplitude'); title('Right channel IR');
legend(rts{1:i});
grid();
hold on;
figure()
plot(t,lIR_SPL); xlabel('Seconds'); ylabel('Amplitude'); title('Left channel IR');
legend(rts{1:i});
grid();
hold on;
end
for i= 1:length(rts)
[IR,fs] = audioread(strjoin([inPath,allWavs(i)],'\'));
lIR = IR(:,1);% Speaker pos = -90
lY = fft(lIR,length(lIR));
freq = fs/2*linspace(0,1,length(rIR)/2+1);% Frequency data
figure()
subplot(4,2,i)
plot(freq,abs(rY(1:length(rIR)/2+1))); xlabel('Frequency'); ylabel('Amplitude');title('Right channel');
hold on
grid();
figure()
subplot(4,2,i)
plot(freq,abs(lY(1:length(lIR)/2+1))); xlabel('Frequency'); ylabel('Amplitude');title('Left channel');
hold on
grid();
end
for i= 1:length(rts)
[IR,fs] = audioread(strjoin([inPath,allWavs(i)],'\'));
lIR = IR(:,1);% Speaker pos = -90
rIR = IR(:,2);% Speaker pos = +90
sigt = length(rIR)/fs;% Calculates the total duration of the audio file
dt = 1/fs;% Bandwidth
t = 0:dt:(length(rIR)*dt)-dt;% Time data
figure(i)
% spectrogram(rIR,dt,length(rIR),fs)
spectrogram(rIR)
end
답변 (1개)
Geoff Hayes
2018년 6월 11일
Maryam - on each iteration of your for loops you seem to be calling
figure()
at least twice. This will create a new figure and so each plot will be on that new figure...instead of each plot (or subplot) being on the same figure.
If you want each of your three for loops to have its own figure, then remove the calls to
figure()
from within your for loops and call it once outside of your loop like
figure();
for i= 1:length(rts)
% etc.
end
If you want one figure for all plots from all iterations of all for loops, then just call figure() once.
댓글 수: 2
Maryam Landi
2018년 6월 11일
Geoff Hayes
2018년 6월 11일
편집: Geoff Hayes
2018년 6월 11일
and you have the hold on? also, please clarify what you mean by overwritten. Do the first two plots get overwritten on subsequent iterations of the loop? Or the second plot overwrite the first? Please show your updated code.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!