Search code examples
matlabplothorizontal-line

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?


Solution

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