n=-20:20;
w=-2.5*pi:.01:2.5*pi;
x1=zeros(size(n));
x1(n>=-2 & n<=2)=1;
% x1(19:23)=1;
Matlab doesn't have a function dtft.
which -all dtft
try
X=dtft(n,x1,w);
catch ME
ME.message
end
Here's one way to generate the DTFT over the frequency range
X = freqz(x1,1,w).*exp(-1j*w*n(1));
Yes, the angle jumps around.
subplot(211);
plot(w,abs(X))
subplot(212);
plot(w,angle(X))
That's because the DTFT as computed by freqz returns very, very, very small imaginary part, presumably due to round off, which causes the phase to be either very close to zero, very close to pi, over very close to -pi
figure
subplot(211),plot(w,real(X))
subplot(212),plot(w,imag(X))