简体   繁体   中英

One of the graphs is not plotting in MATLAB

I have three graphs to plot. They all plot fine alone, but when I want the graphs plotted in separate windows, the second graph is not plotted.

This first graph plots fine:

function wave = carrier( t )
    %The carrier signal is a sine wave
    wave=sin(10*pi*t);
    %Plots the carrier wave
    figure(1);
    plot(wave);
    title('Figure 1: ASK Carrier signal')
    xlabel('Time')
    ylabel('Amplitude')
end

This is the second graph which does not plot:

% Generates the data signal then plots it. The data signal is: 10110100
function [ D ] = data( t )
    %Genereates the data signal
    D=[ones(1,100) zeros(1,100) ones(1,100) ones(1,100) zeros(1,100) ones(1,100) zeros(1,100) zeros(1,100)];
    %Plots the data signal
    figure(2);
    plot(t,D);
    title('Figure 2: Data signal')
    xlabel('Time')
    ylabel('Amplitude')
end

Finally, this is the third graph that plots:

function [ modulated ] = ASK( t )
%Using '.*'to multiply the arrays element by element
    modulated=data(t).*carrier(t);
    figure(3);
    %plots both the ASK and the data signal on the same graph
    plot(t,modulated,t,data(t), 'LineWidth',2);
    title('Figure 3: ASK modulated wave')
    xlabel('Time')
    ylabel('Amplitude')
    legend('ASK (t)','data(t)')
%     for i=1:1:10;
%       %adding noise to simulate real life transmission of data
%         modulated(round(rand(1)*800))=rand(1);
%     end

end

How can I make all three graphs plot fine in separate windows? This is what it looks like: http://prntscr.com/2i75xg BTW, already tried subplot, same thing.

The function data makes figure 2 the active figure. You call data in the plot command in the function ASK which makes figure 2 active. That's why nothing is displayed in figure 3.

You probably want something like this in ASK :

d = data(t);
figure(3)
plot(t, modulated, t, d, 'LineWidth'2);

I was able to make the plots work for all 3 functions, but only if the input vector, t, has a length of exactly 800. This is because in the function data, you hardcode D to have a length of 800:

D=[ones(1,100) zeros(1,100) ones(1,100) ones(1,100) zeros(1,100) ones(1,100) zeros(1,100) zeros(1,100)];

If you pass anything else in, you will receive an error that "vector lengths must match."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM