How to plot a straight line in Matlab. The same code works in GNU-Octave, FreeMat, Scilab and Scicoslab.
You need at least two points and the built-in function plot.
More info at: http://matrixlab-examples.com/horizontal-lines.html
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, 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