MATLAB: Return array of sine waves from Fast Fourier Transform

fftplot

If I have some time series data, is there a convenient way to perform a FFT and return the top 5 largest magnitude sine waves in an array (whether it's the data or equations for them)? I will then plot them, and possibly sum a few of them together to see the results.
Ofcourse I can just do:
X = fft(x);
then find the largest 5 magnitude frequencies manually from the returned data, and get the equations myself.
Just wondering if MATLAB has something already like this built in.
Cheers

Best Answer

Adopted from this MATLAB example: https://www.mathworks.com/help/releases/R2020a/matlab/ref/fft.html#buuutyt-9 following shows how to extract the top five frequencies and their amplitudes.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
X = S + 2*randn(size(t));
Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
[mag5, idx] = maxk(P1, 5); % magnitudes
f5 = f(idx); % frequencies