# How to store the signal in a variable and plot the signal in MATLAB

I am trying to store the signal I have created in a single variable and graph the signal in two subplots in one single graph, one for left channel, one for right channel.

Fs = 44100;
N = 400
t = (1:N)*(1/Fs);
freq = 500;

sound_array = zeros(N,2);
%starts N x 2 matrix
sound_array(:,1) = sin(2*pi*freq*t);
sound_array(:,2) = sin(2*pi*freq*t);

%Left and Right channels
sound_L = sound_array;
sound_L(:,2) = 0;
sound_R = sound_array;
sound_R(:,1) = 0;
plot(sound_array);

R = 400;
beep = [sound_array; zeros(R, 2)];

%Sound 1
sound(sound_L, Fs);
pause(1);
%Sound 2
sound(sound_R, Fs);
%Play Consecutive
sound(sound_array, Fs);

• look at 'help plot' and 'help subplot' – porten May 16 '14 at 19:33
• oh ok i understand now the part for plotting but how do I store the signal in a single varible @porten – Asau7610 May 16 '14 at 19:39
• @Asau7610: I am starting to have a feeling that you are not making any progress on your own. Why won't you try to type into google and play a little bit with MATLAB? It's the easiest programming language ever! – jojek May 16 '14 at 19:43
• I learned the image processing side of matlab just recently for a class so just the whole sound thing is giving me issues for this project since making us learn it on the fly. – Asau7610 May 16 '14 at 19:53

I suggest you to do something like (one plot with two curves):

figure
plot(sound_L(:,1), 'b'); % blue
hold on; grid on; % prevent from redrawing and add grid\
plot(sound_R(:,2), 'r'); % red
legend({'left', 'right'});


Or if you want two subplots:

figure
subplot(211);
plot(sound_L(:,1), 'b'); grid on;
subplot(212);
plot(sound_R(:,2), 'r'); grid on;


Please mind that in your case sound_L and sound_R are the same. You are not making any use of beep variable.

• I thought I had used the beep variable and called @jojek – Asau7610 May 16 '14 at 19:53
• @Asau7610: I think that you need to look at your code and really understand what do you want to do with it. Listen to the results, play with it and then you will move forward. – jojek May 16 '14 at 19:56
• yea im looking at it now trying to make sure and use the beep varible @jojek – Asau7610 May 16 '14 at 20:03
• Since you made the beep varible for the R wouldn't I have to make one for the L too by doing the same thing @jojek ' L = 400;' ' beep = [sound_array; zeros(L, 2)];' – Asau7610 May 16 '14 at 20:15
• sound_array contains both channels in your case. You must substitute it with sound_L or sound_R- and yes, you must create two separate variables (although there is plenty of different valid ways to do what) – jojek May 16 '14 at 20:20