How can I draw multiple vertical lines in math lab?

How can I draw multiple vertical lines in math lab? I want the distance between them 2 and y start from y=1 to y=20 and the x-axis [1:20]

Answers (1)

Try this:
figure(1)
plot([1:2:20; 1:2:20], [20*ones(1,10); ones(1,10)], '-r')
Change the color and line-type to create the vertical lines you want.

2 Comments

what about if I want the distance between them 1?
This will work for that, and for any other values of the parameters you choose:
X_min = 1; % Initial ‘X’ Value
X_max = 20; % Maximum ‘X’ Value
X_step = 1; % Step ‘X’ Value
Lv = numel(X_min:X_step:X_max); % X-Vector Length
Y_min = 1; % Minimum Value For Y-Lines
Y_max = 20; % Maximum Value For Y-Lines
figure(1)
plot([X_min:X_step:X_max; X_min:X_step:X_max], [Y_max*ones(1,Lv); Y_min*ones(1,Lv)], '--r')
You will still need to define the color and line-type (here a red dashed line, defined by '--r'). See the documentation for plot for those options.

Sign in to comment.

Asked:

on 21 Feb 2018

Commented:

on 21 Feb 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!