MATLAB: How to create a box in a 2-D graph that exactly reproduces the box framing of a 3-D axis in MATLAB 7.0.1 (R14SP1)

MATLAB

I would like to be able to plot a box in a 2-D graph that perfectly reproduces the box framing of a 3-D current axis. For example, I can use the VIEWMTX function:
set(gca,'units','normalized')
xc=get(gca,'cameratarget')
hold on
x = [0 1 1 0 0 0 1 1 0 0 1 1 1 1 0 0];
y = [0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 1];
z = [0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0];
[th,ph]=view;
A = viewmtx(th,ph,0,xc);
[m,n] = size(x);
x4d = [x(:),y(:),z(:),ones(m*n,1)]';
x2d = A*x4d;
x2 = zeros(m,n); y2 = zeros(m,n);
x2(:) = x2d(1,:);
y2(:) = x2d(2,:);
plot(x2,y2)
However, it does not perfectly reproduce the box. It seems to be off center.

Best Answer

In order to reproduce the 3-D frame box on a 2-D graph, you need to also set the axes appropriately. You can set the "axis" property to "tight" which sets the axis limits to the range of the data. For example:
for n = 1:100
view(3);
pause
set(gca,'units','normalized');
xc=get(gca,'cameratarget');
x = [0 1 1 0 0 0 1 1 0 0 1 1 1 1 0 0];
y = [0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 1];
z = [0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0];
[th,ph]=view;
A = viewmtx(th,ph,0,xc);
[m,n] = size(x);
x4d = [x(:),y(:),z(:),ones(m*n,1)]';
x2d = A*x4d;
x2 = zeros(m,n); y2 = zeros(m,n);
x2(:) = x2d(1,:);
y2(:) = x2d(2,:);
plot(x2,y2)
axis tight;
pause
cla
end