several for loop plots
Show older comments
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
Answers (1)
Geoff Hayes
on 11 Jun 2018
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 Comments
Maryam Landi
on 11 Jun 2018
Geoff Hayes
on 11 Jun 2018
Edited: Geoff Hayes
on 11 Jun 2018
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.
Categories
Find more on Loops and Conditional Statements 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!