MATLAB: Subplot not combining graphs

graphMATLABsubplot

I'm having trouble with the subplot function. I'm working with this code:
filename = 'UnemploymentVacancySeason.xlsx';
num = xlsread(filename);
subplot(1,2,1);
t1=datetime(2000,12,01);
t2=datetime(2015,05,01);
vr=num(:, 3);
ur=num(:, 4);
nn=t1+calmonths(0:173);
figure;
plot(nn, vr, 'r', nn,ur,'b'); xlim ([t1 t2]); datetick('x',12,'keeplimits');
subplot(1,2,2);
urbefore=num(1:80, 4);
vrbefore=num(1:80, 3);
figure;
plot(urbefore, vrbefore, '-o'); xlim ([3 11]); ylim ([1 4])
hold on
urafter=num(81:174, 4);
vrafter=num(81:174, 3);
plot(urafter, vrafter, '-o');
hold off
If I remove the subplots from the code but keep the rest as is, I get these:
But if I keep the subplots in the code, then I get three separate figures that look like this:
How can I fix my code so that I can get the first two graphs side by side?

Best Answer

The extra figure calls are causing the problem. It creates a new figure, not something you want in the middle of your subplot creation. I commented them out here (just delete them in your code), and it seems to give the result you want:
num = rand(174,4); % Create Data
subplot(1,2,1);
t1=datetime(2000,12,01);
t2=datetime(2015,05,01);
vr=num(:, 3);
ur=num(:, 4);
nn=t1+calmonths(0:173);
% figure;

plot(nn, vr, 'r', nn,ur,'b'); xlim ([t1 t2]); datetick('x',12,'keeplimits');
subplot(1,2,2);
urbefore=num(1:80, 4);
vrbefore=num(1:80, 3);
% figure;
plot(urbefore, vrbefore, '-o'); xlim ([3 11]); ylim ([1 4])
hold on
urafter=num(81:174, 4);
vrafter=num(81:174, 3);
plot(urafter, vrafter, '-o');
hold off
If you want to create a new figure (good programming practise in my opinion), call figure it before your subplot calls:
figure
subplot(2,1,1)
...
subplot(2,1,2)
...