How to draw horizontal and vertical lines in MATLAB?

Chris Jones picture Chris Jones · Feb 5, 2015 · Viewed 8.3k times · Source

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?

Answer

TroyHaskin picture TroyHaskin · Feb 5, 2015

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);