My plots are getting overidded by other plots. How do I fix this issue?

1 次查看(过去 30 天)
This is used to determine the Fourier Series representation
clear
clc
%Making the original plot and defining points
N = 1024;
dt = 2/(N-1);
t = 0:dt:2;
ft = zeros(1,length(t));
ft(1:N/2-1)= 2;
ft(N/2:end)= -2;
%Plotting points
figure
plot(t,ft,LineWidth=2)
title("Original Plot f(t) v/s t")
xlabel("t")
ylabel("f(t)")
grid
xlim([t(1)-1,t(end)+1])
%Finding fourier series using the below function
FourierSeries_Calculator(ft,t,dt,100)
function FourierSeries_Calculator(ft,t,dt,n)
arguments
ft (1,:) double
t (1,:) double
dt (1,1)
n (1,1) {mustBeNumeric,mustBePositive,mustBeGreaterThan(n,20)}
end
%Used to calculate the fundamental time period
T0 = t(end)-t(1);
w0 = 2*pi/T0;
%Finding dc value,phase and magnitude components
[a,w,phase] = deal(zeros(1,2*n+1));
for m = -n:n
a(m+n+1) = sum(ft.*exp(-1i*m*w0.*t))*dt/T0;
w(m+n+1) = m*w0;
phase(m+n+1) = angle(a(m+n+1));
end
%Plotting Phase and Magnitude spectrum
figure
stem(w,abs(a))
title("Magnitude Spectrum")
xlabel("w0")
ylabel("|a_n|")
grid
figure
stem(w,phase)
title("Phase Spectrum")
xlabel("w_0")
ylabel("Phase (in radians)")
grid
FourierSeries_Plot(a,t,w0,n)
end
function FourierSeries_Plot(a,t,w0,n)
arguments
a (1,:)
t (1,:)
w0 (1,1) {mustBePositive}
n (1,1) {mustBePositive}
end
%An animated plot, once the for loop is completed, the plot is cleared and
%the Final fourier Series representation is displayed
FourierS = 0;
figure
for m = -n:n
FourierS = FourierS + a(n+m+1)*(exp(1i*w0*m.*t));
plot(t,real(FourierS),LineWidth=2)
pause(0.01)
end
clf
plot(t,real(FourierS),LineWidth=2)
title("Fourier Series Representation")
xlabel("t")
grid
end
%OUTPUT
%This same figure appears for the first 3 figures
%Only the last figure shows the correct output

采纳的回答

Epsilon
Epsilon 2025-6-16
Hi Prisma,
The initial figures may be getting unintentionally updated because the plot and stem functions in the code rely on gcf (which returns the current figure handle) to display the data. As a result, even after the correct values are plotted initially, the figures might change later in the run. This can occur for various reasons, one common cause is manually selecting a figure by clicking on it, which makes it the current figure and redirects subsequent plots to it.
To avoid such issues, it's advisable to define the figures and axes explicitly for each graph. A basic example of the syntax would be:
figN=figure(N)
axN=axes('parent',figN);
stem(axN,_,_);
plot(axN,_,_);
Please also find the implemented code with the correct desired figures:
clear
clc
N = 1024;
dt = 2/(N-1);
t = 0:dt:2;
ft = zeros(1,length(t));
ft(1:N/2-1)= 2;
ft(N/2:end)= -2;
%% defining the figure and the axes explicitly
fig1=figure(1);
ax1=axes('parent',fig1);
plot(ax1,t,ft,LineWidth=2) %plotting to the specific axes
title("Original Plot f(t) v/s t")
xlabel("t")
ylabel("f(t)")
grid
xlim([t(1)-1,t(end)+1])
%Finding fourier series using the below function
FourierSeries_Calculator(ft,t,dt,100);
function FourierSeries_Calculator(ft,t,dt,n)
arguments
ft (1,:) double
t (1,:) double
dt (1,1)
n (1,1) {mustBeNumeric,mustBePositive,mustBeGreaterThan(n,20)}
end
T0 = t(end)-t(1);
w0 = 2*pi/T0;
[a,w,phase] = deal(zeros(1,2*n+1));
for m = -n:n
a(m+n+1) = sum(ft.*exp(-1i*m*w0.*t))*dt/T0;
w(m+n+1) = m*w0;
phase(m+n+1) = angle(a(m+n+1));
end
fig2=figure(2);
ax2=axes('parent',fig2);
stem(ax2,w,abs(a))
title("Magnitude Spectrum")
xlabel("w0")
ylabel("|a_n|")
grid
fig3=figure(3);
ax3=axes('parent',fig3);
stem(ax3,w,phase)
title("Phase Spectrum")
xlabel("w_0")
ylabel("Phase (in radians)")
grid
FourierSeries_Plot(a,t,w0,n);
end
function FourierSeries_Plot(a,t,w0,n)
arguments
a (1,:)
t (1,:)
w0 (1,1) {mustBePositive}
n (1,1) {mustBePositive}
end
FourierS = 0;
%% defining the figure and the axes explicitly
fig4=figure(4);
ax4=axes('parent',fig4);
for m = -n:n
FourierS = FourierS + a(n+m+1)*(exp(1i*w0*m.*t));
plot(ax4,t,real(FourierS),LineWidth=2) %plotting to the specific axes
pause(0.01)
end
clf(fig4)
fig4=figure(4);
ax4=axes('parent',fig4);
plot(ax4,t,real(FourierS),LineWidth=2)
title("Fourier Series Representation")
xlabel("t")
grid
end
%OUTPUT
Using clf without an argument clears the current figure, which may not be the intended one. To avoid this, it’s recommended to pass the specific figure handle to clf.
Hope this helps.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by