MATLAB: How to create a box inside box

boxdivisiongridmeshquadtree

I am trying to create a large box (Four division) and further divide individual box into four small boxes. I am able to divide the boxes on bottom left and top right portion but not in top left and bottom right portion, can anyone please help me on this. I have attached the MATLAB code and also the image for reference.
Thank you
The code:
clear
clc
nx=3;
ny=3;
x=linspace(0,1,nx);
y=linspace(0,1,ny);
[X,Y]=meshgrid(x,y);
%%%%%%%%%%%%%%%%%%%


x1=linspace(0,1/2,nx);
y1=linspace(0,1/2,ny);
[X1,Y1]=meshgrid(x1,y1);
%%%%%%%%%%%%%%%%%%%
x2=linspace(0,1/4,nx);
y2=linspace(0,1/4,ny);
[X2,Y2]=meshgrid(x2,y2);
%%%%%%%%%%%%%%%%%%%
x3=linspace(1/2,1,nx);
y3=linspace(1/2,1,ny);
[X3,Y3]=meshgrid(x3,y3);
plot(X,Y,'k',Y,X,'k');
hold on
plot(X1,Y1,'k',Y1,X1,'k');
hold on
plot(X2,Y2,'k',Y2,X2,'k');
hold on
plot(X3,Y3,'k',Y3,X3,'k');

Best Answer

It would have been easier if you'd attached the drawing of what your're finally trying to achieve as the description is quite unclear. My suggestion is that you swap plot function for mesh and then as long as you can generate the (x,y) coordinates for for the square you're trying to plot - mesh function will plot them.
See the example below (based on your code):
clear all;
clc;
nx=5;
ny=5;
% MAIN BOX
x=linspace(0,1,nx);
y=linspace(0,1,ny);
[X,Y]=meshgrid(x,y);
Z = zeros(size(X));
mesh(X,Y,Z,...
'LineStyle','-',...
'edgecolor','r')
% https://uk.mathworks.com/help/matlab/ref/view.html?searchHighlight=view&s_tid=doc_srchtitle
view(2)
hold on
% SUB BOX
x1=linspace(0.25,0.75,nx);
y1=linspace(0.25,0.75,ny);
[X1,Y1]=meshgrid(x1,y1);
Z1 = zeros(size(X1));
mesh(X1,Y1,Z1,...
'LineStyle','--',...
'edgecolor','b')
% SUB-SUB BOX
x2=linspace(0.25+0.125, 0.75-0.125,nx);
y2=linspace(0.25+0.125, 0.75-0.125,ny);
[X2,Y2]=meshgrid(x2,y2);
Z2 = zeros(size(X2));
mesh(X2,Y2,Z2,...
'LineStyle','-.',...
'edgecolor','g')
hold off
box off
Related Question