How to Plot a Straight Line in
Matlab
This also works in GNU-Octave,
FreeMat, Scilab and Scicoslab
You can plot a straight line just as you would plot
any other function in Matlab.
The basic use of the built-in function plot is:
plot(x, y)
where
x = array of x-values
y = array of y-values
For example, plot this line:
(3, 4)
(-1, 2)
(3, 4)
(-1, 2)
% create your x and y-arrays
x = [-1 3];
y = [2 4];
% just plot the line:
plot(x, y)
So this short code:
x = [-1 3];
y = [2 4];
plot(x, y)
produces this plot:
You can plot more that one line by using the hold on
command once (after the first plot)…
plot([-1 3], [2 4])
hold on
plot([1 1], [5 2])
produces this plot
You can have different colors and widths
plot([-1 3], [2 4], ‘r’, ‘Linewidth’, 2)
hold on
plot([1 1], [5 2], ‘k’, ‘Linewidth’, 3)
produces this plot
For alternative codes, examples and
details, visit:
matrixlab-examples.com/horizontal-lines.html

Plot a straight line in Matlab

  • 1.
    How to Plota Straight Line in Matlab This also works in GNU-Octave, FreeMat, Scilab and Scicoslab
  • 2.
    You can plota straight line just as you would plot any other function in Matlab. The basic use of the built-in function plot is: plot(x, y) where x = array of x-values y = array of y-values
  • 3.
    For example, plotthis line: (3, 4) (-1, 2)
  • 4.
    (3, 4) (-1, 2) %create your x and y-arrays x = [-1 3]; y = [2 4]; % just plot the line: plot(x, y)
  • 5.
    So this shortcode: x = [-1 3]; y = [2 4]; plot(x, y) produces this plot:
  • 6.
    You can plotmore that one line by using the hold on command once (after the first plot)… plot([-1 3], [2 4]) hold on plot([1 1], [5 2]) produces this plot
  • 7.
    You can havedifferent colors and widths plot([-1 3], [2 4], ‘r’, ‘Linewidth’, 2) hold on plot([1 1], [5 2], ‘k’, ‘Linewidth’, 3) produces this plot
  • 8.
    For alternative codes,examples and details, visit: matrixlab-examples.com/horizontal-lines.html