How to print changing y value on plot using the "COMET" function

5 ビュー (過去 30 日間)
Michael Armstrong
Michael Armstrong 2018 年 12 月 1 日
コメント済み: Walter Roberson 2018 年 12 月 2 日
For the following code I am trying to print the y value as a function of time on the plot itself (shown here is a damped sine wave function). However, the value is not printing. Many thanks in advance for any help.
x = linspace(0,5,250);
for k = 2:length(x)
y = exp(-350.*x).*25.*cos(4.*x)+200;
ax = axes;
set(ax, 'xlim', [0 3], 'ylim', [150 300]);
hold(ax);
comet(ax,x,y)
txt = num2str(y); %%%%%%ISSUE CODE
text(2.15,275,sprintf('%d',txt)) %%%%%%ISSUE CODE
end

採用された回答

Walter Roberson
Walter Roberson 2018 年 12 月 1 日
text(2.15,275,txt)
Alternately instead of assigning anything to txt use
text(2.15,275,sprintf('%d',y))
  2 件のコメント
Michael Armstrong
Michael Armstrong 2018 年 12 月 1 日
Thanks again, Walter Robinson. The text is appearing behind the background of the plot, and I should include "delete(y)" at the end of the loop. I'll comment again once I've fixed the issue.
Walter Roberson
Walter Roberson 2018 年 12 月 2 日
if exist('lab', 'var') && ishghandle(lab); delete(lab); end
lab = text(2.15,275,sprintf('%d',y));
WIth regards to it appearing behind the plot: use a z coordinate in the text() call.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by