How to create a box inside box?
5 次查看(过去 30 天)
显示 更早的评论
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');
0 个评论
采纳的回答
Pawel Jastrzebski
2018-1-16
编辑:Pawel Jastrzebski
2018-1-16
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
4 个评论
Pawel Jastrzebski
2018-1-16
编辑:Pawel Jastrzebski
2018-1-16
Firstly, this should be:
nx=5;
ny=5;
You also don't need hold on after every plot. One 'hold on' is all it takes to keep all of the graphs in one figure.
Now, the main reason your code doesn't work is that the range you specified within the linspace function for different 'squares' are incorrect.
The easiest way to spot it is when you create one set of data at the time, plot it and then add another one. I've colour-coded the steps in the code below, maybe this way it's easier to follow:
nx=5;
ny=5;
% DATA 1
x=linspace(0,1,nx);
y=linspace(0,1,ny);
[X,Y]=meshgrid(x,y);
% PLOT - STEP 1 (RED and BLUE)
plot(X,Y,'r');
hold on % ← use only once, not before every 'plot'!
plot(Y,X,'b');
% DATA 2
x1=linspace(0.25,0.75,nx);
y1=linspace(0.25,0.75,ny);
[X1,Y1]=meshgrid(x1,y1);
% PLOT - STEP 2 (GREEN)
plot(X1,Y1,'g--');
% DATA 3
x2=linspace(0.25+0.25/2,0.75-0.25/2,nx);
y2=linspace(0.25+0.25/2,0.75-0.25/2,ny);
[X2,Y2]=meshgrid(x2,y2);
% PLOT - STEP 3 (BLACK)
plot(X2,Y2,'k');
plot(Y2,X2,'k');
hold off
box off
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!