MATLAB Answers

How to make the program more flexible

1 view (last 30 days)
han han
han han on 26 Jun 2020
Commented: han han on 27 Jun 2020
I want to make a frame that can follow the pattern.
ex, M = 4,N = 4, I would got a 4 by 4 "x" (matrix)
However, this Mg by Ng frame will surround the M by N matrix
But my program is not flexible enough and the problem is in the frame settings, but I don’t know how to modify...
M = 4; % "\" pattern and "/" pattern (column)
N = 4; %"\" pattern and "/" pattern (Row)
Mg = 1; % Frame size (column)
Ng = 1; % Frame size (Row)
a = [1 0.5];
b = [0.5 1];
for q = 1:1:M*Mg
hold on;
for w = 1:1:N*Ng
hold on;
plot(a+q,b+w,'r') % "\" pattern
end
end
hold on;
a1 = [1 0.5];
b1 = [1 0.5];
for q = 1:1:M*Mg
hold on;
for w = 1:1:N*Ng
hold on;
plot(a1+q,b1+w,'b') % "/" pattern
end
end
%---------Frame size--------------
for M = 0 : M: M*Mg
hold on;
x=[1.25 1.25 1.25+M 1.25+M 1.25];
for N = 0 : N: N*Ng
y=[1.25 1.25+N 1.25+N 1.25 1.25];
plot(x,y,'k','lineWidth',2)
end
end
Hope to present the following fig, M = 4; N = 4; Mg = 2; Ng = 2;

  0 Comments

Sign in to comment.

Accepted Answer

Tommy
Tommy on 26 Jun 2020
Here's a function that places an M x N grid of Xs within a rectangle defined by pos, as well as a few lines which use that function to create your frames.
M = 8; % "\" pattern and "/" pattern (column)
N = 8; %"\" pattern and "/" pattern (Row)
Mg = 2; % Frame size (column)
Ng = 2; % Frame size (Row)
ax = axes('NextPlot', 'add',...
'XTick', [],...
'YTick', []);
for ii = 1:Mg
for jj = 1:Ng
pos = [ii, jj, 1, 1];
fillSquareWithXs(ax, pos, M, N)
end
end
function fillSquareWithXs(ax, pos, M, N)
% ax - target axes
% pos - position of square, [llx, lly, width, height]
% M - # of rows of Xs
% N - # of cols of Xs
% find location, width, and height of each X
xco = linspace(pos(1), pos(1)+pos(3), M+1);
yco = linspace(pos(2), pos(2)+pos(4), N+1);
dx = diff(xco)/2;
dy = diff(yco)/2;
xco = xco(1:end-1) + dx;
yco = yco(1:end-1) + dy;
% plot Xs
for q = xco
for w = yco
plot(ax,q+[dx/2 -dx/2],w+[-dy/2 dy/2],'r') % "\" pattern
plot(ax,q+[dx/2 -dx/2],w+[dy/2 -dy/2],'b') % "/" pattern
end
end
% plot borders
plot([pos(1), pos(1)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2)+pos(4), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2), pos(2)], 'k', 'LineWidth', 2);
plot([pos(1)+pos(3), pos(1)+pos(3)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
end

  4 Comments

Show 1 older comment
han han
han han on 27 Jun 2020
It seems that your function is only applicable to the case of M = N. Is there a way to make the case of M ~= N also work?
Tommy
Tommy on 27 Jun 2020
Happy to help! Good point, try this:
M = 4; % "\" pattern and "/" pattern (column)
N = 8; %"\" pattern and "/" pattern (Row)
Mg = 2; % Frame size (column)
Ng = 2; % Frame size (Row)
ax = axes('NextPlot', 'add',...
'XTick', [],...
'YTick', []);
for ii = 1:Mg
for jj = 1:Ng
pos = [ii, jj, 1, 1];
fillSquareWithXs(ax, pos, M, N)
end
end
function fillSquareWithXs(ax, pos, M, N)
% ax - target axes
% pos - position of square, [llx, lly, width, height]
% M - # of rows of Xs
% N - # of cols of Xs
% find location, width, and height of each X
xco = linspace(pos(1), pos(1)+pos(3), M+1);
yco = linspace(pos(2), pos(2)+pos(4), N+1);
dx = (xco(2)-xco(1))/2; % <--- changed
dy = (yco(2)-yco(1))/2; % <--- lines
xco = xco(1:end-1) + dx;
yco = yco(1:end-1) + dy;
% plot Xs
for q = xco
for w = yco
plot(ax,q+[dx/2 -dx/2],w+[-dy/2 dy/2],'r') % "\" pattern
plot(ax,q+[dx/2 -dx/2],w+[dy/2 -dy/2],'b') % "/" pattern
end
end
% plot borders
plot([pos(1), pos(1)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2)+pos(4), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2), pos(2)], 'k', 'LineWidth', 2);
plot([pos(1)+pos(3), pos(1)+pos(3)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
end
han han
han han on 27 Jun 2020
I can't thank you enough.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by