Hold on function doesn't work

Hello guys,
I'm at the end of my wits here.
I need to add a dashed line at a constant to a plot using the hold on function.
This is the function: V(t) = V_rest+I.*R.*(1-exp(1).^(t/theta)).
Plotting it works, but then I need to add to constant horizontal dashed line, preferably using "hold on" function. Nothing I do works.
Can any one help? (I have R2020b)

3 件のコメント

the cyclist
the cyclist 2020 年 10 月 29 日
編集済み: the cyclist 2020 年 10 月 29 日
The hold function works.
We'll need to see your code, to determine why it is not doing what you expect. You can upload it here using the INSERT icon that looks like a paper clip.
the cyclist
the cyclist 2020 年 10 月 29 日
It would also be helpful if you told us sample inputs that you used, instead of us needing to guess at that.
Shiri Ben Artzi
Shiri Ben Artzi 2020 年 10 月 29 日
only restrictions are V_rest<0, R, theta>0
tried many options, all won't work
exmp ex1_ID1_ID2_Q4(-0.5, 2, 2.5, -0.3, 1.5)

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

回答 (3 件)

Image Analyst
Image Analyst 2020 年 10 月 29 日

1 投票

If you use yline() you don't need to use hold on. If you use plot(), you DO need to use hold on. Try this demo which shows you both ways to do it:
% First set up the plot.
t = linspace(0, 1, 1000);
theta = 1;
V_rest = -1;
R = 1;
I = 1;
V = V_rest+I.*R.*(1-exp(1).^(t/theta));
plot(t, V, 'b-', 'LineWidth', 2);
grid on;
title('V vs. t', 'FontSize', 18);
xlabel('t', 'FontSize', 18);
ylabel('V', 'FontSize', 18);
% Now plot is setup and we can draw the horizontal lines at a constant value.
% Option 1 : plot red dashed line at V = -2 using yline(). "hold on" is NOT needed if you use yline().
yline(-2, 'LineStyle', '--', 'Color', 'r', 'LineWidth', 2);
% Option 2 : plot magenta dashed line at V = -2.5 using plot(). In this case, "hold on" is needed.
hold on;
plot(xlim, [-2.5, -2.5], 'm--', 'LineWidth', 2);
hold off;
Michael Houston
Michael Houston 2020 年 10 月 29 日

0 投票

Try refline() or yline() instead of plot().

3 件のコメント

Shiri Ben Artzi
Shiri Ben Artzi 2020 年 10 月 29 日
tried, but i need a dashed line, not sure how to defined it with the yline (tried and nut succsseded)
and anyway i cant understand why the hold on wont work :\
Michael Houston
Michael Houston 2020 年 10 月 29 日
yline(tau,'lineStyle','--')
Shiri Ben Artzi
Shiri Ben Artzi 2020 年 10 月 29 日
thank you!

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

the cyclist
the cyclist 2020 年 10 月 29 日

0 投票

The problem with your code is not with the hold command. The problem is the syntax to plot the line. You plotted a 1x200 vector against a scalar, which does not do what you expect.
Try this instead:
plot(t,tau*ones(size(t)),'LineWidth',2)
I increased the line width so that it would show up more prominently.

カテゴリ

ヘルプ センター および File ExchangeLabels and Styling についてさらに検索

製品

リリース

R2020b

タグ

質問済み:

2020 年 10 月 29 日

回答済み:

2020 年 10 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by