user3830162
user3830162

Reputation:

Modify the mesh grid in matlab

I want to have grid with lines in it like this:

enter image description here

Currently, i have coded to get grid like this:

enter image description here

I just need to add horizontal and vertical lines in it.

MyCode:
[X,Y] = meshgrid(-1:0.1:1, -1:0.1:1);
X = X(:);
Y = Y(:);
 plot(X,Y,'b.');
xlabel('X'); % // Label the X and Y axes
ylabel('Y');
title('Initial Grid');

Upvotes: 1

Views: 2565

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

To draw those lines, the easiest approach is two loops:

x = -1:0.1:1;
y = -1:0.1:1;
hold on
for n = 1:numel(x); %// loop over vertical lines
    plot([x(n) x(n)], [y(1) y(end)], 'k-'); %// change 'k-' to whatever you need
end
for n = 1:numel(y); %// loop over horizontal lines
    plot([x(1) x(end)], [y(n) y(n)], 'k-'); %// change 'k-' to whatever you need
end

enter image description here


Alternatively, you can use grid; but then you don't have control on line type. You get black dotted lines:

x = -1:0.1:1;
y = -1:0.1:1;
figure
set(gca,'xtick',x);
set(gca,'ytick',y);
axis([min(x) max(x) min(y) max(y)])
grid on

enter image description here

Upvotes: 2

Related Questions