Hi Heather,
Based on your description, I understand that you are facing an issue with the plot obtained from the Fourier Transform of the boxcar function. Upon reviewing your code, I can identify that the reason for the unexpected plot is that you are getting is the real part of "X(r)". To visualize the sinc wave's magnitude, you should plot the magnitude (absolute value) of "X(r)" instead.
Here's the corrected code segment:
t=linspace(-10,10,20480);
x=heaviside(t+0.5)-heaviside(t-0.5);
Fs=1024;
NFFT=length(t);
r=[10141:10341];
fVals=Fs*(-NFFT/2:NFFT/2-1)/NFFT;
X=fft(x);
X=fftshift(X);
figure
plot(fVals(r),abs(X(r)))
title('Magnitude FFT of x(t) from -5 to 5 Hz')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
By plotting the absolute value of "X(r)", you will see the magnitude of the sinc function. Note that this reflects the magnitude of the Fourier Transform, which same as the sinc signal's magnitude. To interpret information about the signal's negative and positive amplitude, you have to examine the phase component.
Hope this helps!