Chris Jones
Chris Jones

Reputation: 672

How to draw horizontal and vertical lines in MATLAB?

I am currently trying to plot a simple vertical and horizontal lines in MATLAB.

For instance, I would like to plot the line y=245. How would I do that?

Upvotes: 2

Views: 11996

Answers (3)

Paolo
Paolo

Reputation: 26285

Since MATLAB R2018b you can use the functions xline and yline:

>> yline(245);

Upvotes: 1

scmg
scmg

Reputation: 1894

2 simple ways:

plot(0:0.001:1, 25);

line('XData', [0 1], 'YData', [25 25]);

Upvotes: 1

TroyHaskin
TroyHaskin

Reputation: 8401

MATLAB's plotting works on a point-by-point basis from the vectors you give. So to create a horizontal line, you need to varying x while keeping y constant and vice-versa for vertical lines:

xh = [0,10];
yh = [245,245]; % constant

xv = [5,5]; % constant
yv = [0,245*2];

plot(xh,yh,xv,yv);

Upvotes: 4

Related Questions