OneZero
OneZero

Reputation: 11904

Vertical refline in matlab?

How do I draw a vertical refline in matlab? e.g. I want to plot a line of x=5. Obviously using inf does not help at all. Can anyone give some advice?

Upvotes: 3

Views: 29083

Answers (5)

Cris Luengo
Cris Luengo

Reputation: 60444

Since MATLAB R2018b there is xline for this purpose:

 xline(0)

draws a vertical line at x==0.

Upvotes: 3

user3484718
user3484718

Reputation: 11

The function refline lets you specify gradient and intercept.

Upvotes: 1

Jakob Heiden
Jakob Heiden

Reputation: 11

You can use refline and then edit the XData and YData properties to create a vertical line.

Upvotes: 1

James McCorrie
James McCorrie

Reputation: 2652

There is an excellent answer over on https://stackoverflow.com/a/8108766/1194420 repeated below for convenience. (Please go there an up vote the original answer) ---

There exist an undocumented function graph2d.constantline:

plot(-2:5, (-2:5).^2-1)
%# vertical line
hx = graph2d.constantline(0, 'LineStyle',':', 'Color',[.7 .7 .7]);
changedependvar(hx,'x');
%# horizontal line
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
changedependvar(hy,'y');

Upvotes: 1

Molly
Molly

Reputation: 13610

You can create a vector with many identical values for x. Something like this:

x = 5*ones(1,100);
y = 1:100;
plot(x,y)

or use the line function:

line([5,5],[0,10])

To automatically detect the range of line, use ylim:

plot(1:10)

line([5,5],ylim)

Upvotes: 10

Related Questions