DFT for different data lengths
The information about the frequency of the sine seems to be missing. This could be an hint for you to compute the DFT exactly (not with Matlab simulation), for a given frequency $f$ and a number $s$ of seconds. Since the sampling frequency $f_0$ gives a integer multiple of samples inside one second, you should be able to find manageable formulas.

As commented by @Juancho, there is a lack of homogeneity in your time base: a frequency times a duration in seconds is not homogeneous to a time (it is unitless). A flexible solution is:

fs=10; % sampling freq
lFreq = [3 3.5 4]; % list of frequencies
lDuration = [1 2 4 10]'; % list of durations

for iDuration = 1:length(lDuration)
    timeBase = [0:1/fs:lDuration(iDuration)]';
    dataCosine = cos(timeBase*lFreq*2*pi); % Three cosines
    data = sum(dataCosine,2); % sum the three cosine components
    figure(iDuration)
    plot(timeBase,data,'x-');
    ylabel('Amplitude (a.u.)')
    xlabel('Time (seconds)')
    title(['Duration: ',num2str(lDuration(iDuration)),' s'])
    grid on
end