MATLAB: Changing a figure to a subplot

creatingfiguresubplot

Currently stuck… This code below creates a figure and three subplots as I have designed it too. I cannot figure out how to change the figure in the while loop to a subplot with the other subplots below.
Any help is greatly appreciated.
%--------------------Value Function Interation-----------------------------
%Start Timer
tic
Vold=zeros(1,n);
Iteration = 0;
error = 1;
while error>tol
[Vnew g] = max(utility+beta*Vold'*ones(1,n));
error = max(abs(Vold - Vnew))
Vold = Vnew;
Iteration = Iteration + 1
%Set up convergence graphics -- optional
hold on;
plot(kgrid,Vold)
xlabel('k')
ylabel('V(k) Iterations')
title('The Convergence Process')
hold off;
end
k= kgrid;
kp = kgrid(g);
c = f(k)+(1-delta)*k-kp;
%End Timer
toc
%---------------------Create Plots for Results----------------------------
%Plot
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)*1/4 scrsz(4)*1/4 scrsz(3)*1/2 ...
scrsz(4)*1/2]);
subplot(2,2,1)
plot(kgrid,Vold)
xlabel('k');
ylabel('V(k)');
title('Value Function');
subplot(2,2,2)
plot(kgrid,[kgrid; step*g]);
xlabel('k');
ylabel('g(k)');
title('Policy Function');
legend('45 degree-line','Policy Function','Location','Best');
subplot(2,2,3)
plot(kgrid,c);
xlabel('k');
ylabel('c(k)');
title('Consumption');

Best Answer

I can neither see any mistake in your plotting code nor reproduce your problem. However, better not rely on the current handle. An example in the doc says:
Create a figure with two subplots and return the subplot axes handles, ax1 and ax2.
figure
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
Store the data from the peaks function in matrix Z. Plot the first 20 rows of Z in the upper subplot using its handle, ax1. Plot the entire data set in the lower subplot using its handle, ax2.
Z = peaks;
plot(ax1,Z(1:20,:))
plot(ax2,Z)
Thus:
Try replace your
subplot(2,2,1)
plot(kgrid,Vold)
by
ax1 = subplot(2,2,1);
plot( ax1, kgrid,Vold)
etc.