how can I plot several vertical lines in graph?

93 ビュー (過去 30 日間)
Stephen
Stephen 2016 年 9 月 25 日
編集済み: Adam Danz 2022 年 12 月 16 日
I would like to plot vertical lines at several specified points (x axis) on a graph. Each point is the onset of a stimulus. I specify the number of these points and where they occur with the following:
stimulus = zeros([1 20]);
prompt = ['how many stimuli in this experiment? '];
stimnumber = input(prompt);
for stim=1:stimnumber
query=['when is onset of stim number ',num2str(stim)];
disp(query);
prompt = '? ';
stimulus(1,stim)=input(prompt);
end
This sets up an array ("stimulus") with up to 20 stimulus points I can specify.
Now how can I access "stimulus" to draw the vertical lines? I was able to do this with with a super klugey script such as:
y = [0 5];
x = [stimulus(1,1), stimulus(1,1)];
plot(x,y,'Color','b');
x = [stimulus(1,2), stimulus(1,2)];
plot(x,y,'Color','b');
x = [stimulus(1,3), stimulus(1,3)];
plot(x,y,'Color','b');
.
.
.
%%and so forth up to
x = [stimulus(1,20), stimulus(1,20)];
plot(x,y,'Color','b');
Surely there is a cleaner way to do this with a for loop, but I just can't get it.
Any help?
Here's what it should look like, for instance if I specify 3 stimuli at x=300,400, and 450:

採用された回答

Star Strider
Star Strider 2016 年 9 月 25 日
If ‘stimulus’ is a row vector, this may do what you want:
stimulus = rand(1, 10);
figure(1)
plot([stimulus; stimulus], repmat(ylim',1,size(stimulus,2)), '-k')
  2 件のコメント
Stephen
Stephen 2016 年 9 月 25 日
Thanks, Star Strider. Also, I just figured out how to do it with a for loop. Not as clean as your suggestion, though!
Star Strider
Star Strider 2016 年 9 月 25 日
My pleasure!
I noticed that in the code you posted, you don’t use either the figure (creates new figure window) or hold (keeps the current figure window open for plotting) functions.
The problem is that your code as you posted it will overwrite earlier plots with subsequent ones.
If you’re having problems seeing everything you want to on your plots, see the documentation on figure and hold.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2016 年 9 月 25 日
Try line()
line([x, x], ylim, 'Color', 'b', 'LineWidth', 2);
where x is a single value. If you have a bunch and want to do it all in one call, use stem() and turn the markers off.
stem(x, y, 'MarkerFaceColor', 'none', 'MarkerEdgeColor', 'none', 'LineWidth', 2);
  1 件のコメント
Stephen
Stephen 2016 年 9 月 25 日
Thanks, Image Analyst. I'll try this and Star Strider's method below.

サインインしてコメントする。


Adam Danz
Adam Danz 2020 年 9 月 10 日
編集済み: Adam Danz 2022 年 12 月 16 日
In Matlab r2018b or later, you can use xline() to plot vertical lines or yline for horizontal lines.
In MATLAB R2021a or later
xline and yline accept a vector of values to plot multiple lines
Specify the line style or add a line label using the 2nd and 3rd inputs xline(xvalue,LineSpec,label).
x = [1 5 9];
h = xline(x);
In MATLAB R2018b - R2020b
xline and yline accept scalar values only. Here are two options to plot multiple constant lines:
Option 1: Loop through each value in x
% x is a vector|array of x-values
h = gobjects(size(x));
for i = 1:numel(x)
h(i) = xline(x(i));
end
Option 2: use an array function
% x is a vector|array of x-values
h = arrayfun(@(a)xline(a),x);
Before MATLAB R2018b
Use a different answer in this thread. stem, line, and plot may all come in handy but the line they draw will differ from constant lines in that they will be bounded.
  4 件のコメント
Liam Booth
Liam Booth 2020 年 10 月 12 日
Stephen is obviously a dedicated MATLAB enthusiast!
Zong-Jhen Ye
Zong-Jhen Ye 2022 年 12 月 16 日
編集済み: Zong-Jhen Ye 2022 年 12 月 16 日
Thanks Adam, the matrix method you mentioned is clean and it works.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by