matlab 2015 returns an empty graph

1 回表示 (過去 30 日間)
george mobol
george mobol 2016 年 4 月 20 日
コメント済み: Mike Garrity 2016 年 4 月 25 日
i keep getting an emplty graph figure when plotting using these specific codes on matlab 2015 version unless i bolden and change the color of the plot. However the codes run and plot on lower versions of matlab, 2011 and 2012 specifically without having to bolden and change the graph color. these are codes. dt=1/1000; t=0:dt:6; g=9.81; h=2; u=0; for t=0:dt:6; v = u + (g*dt); dh=0.5*(u+v)*dt; h1 = h-dh; h = h1; u=v; plot (t,h); hold on; end

採用された回答

Mike Garrity
Mike Garrity 2016 年 4 月 20 日
This code is creating 6,001 line objects which each have a single vertex. A line object with a single vertex is a degenerate case which isn't guaranteed to draw anything. Different renderers in different versions of MATLAB have handled this case differently, so this has never been a very reliable way to plot.
One option is to tell plot that you're drawing dots instead of lines. That's as easy as changing your call to plot to look like this:
plot(t,h,'.')
But creating 6,001 objects which each draw a single dot isn't a very efficient way to go, for this reasons I explained in this post on the MATLAB Graphics blog . You'd really be better off with a single call to plot. In your case, it would look something like this.
dt=1/1000;
t=0:dt:6;
g=9.81;
prev_h=2;
u=0;
t = 0:dt:6;
h = zeros(size(t));
for i=1:numel(t)
v = u + (g*dt);
dh = 0.5*(u+v)*dt;
h1 = prev_h-dh;
h(i) = h1;
prev_h = h(i);
u = v;
end
plot(t,h,'.')
The other advantage of this approach is that you could draw lines connecting the points instead of individual dots. With your current approach you can't do that because the plot command can't "see" more than a single data value at a time. By passing all of the data values into plot together, it can connect them.
  2 件のコメント
george mobol
george mobol 2016 年 4 月 25 日
Thank you for the response. i am however want to modify this code to produce an animation which i intend to convert to a movie file in .avi format. i ran the code this way but not getting the desired output. dt=1/1000; g=9.81; u=0; t = 0:dt:7; h = zeros(size(t)); for i=1:numel(t) v = u + (g*dt); dh = 0.5*(u+v)*dt; h1 = H-dh;
if h1<0;
h1=0;
v = -u*CR ;
else
u = v;
end
h(i) = h1;
H = h(i);
u=v;
end
for n= 1:10;
plot(t,h,'ro')
axis equal;
axis([0,10,0,10]);
M(n) = getframe(gcf);
end
movie2avi(M,'myfiles', 'compression', 'none')
Mike Garrity
Mike Garrity 2016 年 4 月 25 日
In that case, you'd want to create an empty plot first, and then append to its XData & YData in the loop. Something like this:
dt=1/1000;
g=9.81;
h=2;
u=0;
hobj = plot(nan,nan);
xlim([0 6])
ylim([-180 20])
for t=0:dt:6
v = u + (g*dt);
dh = 0.5*(u+v)*dt;
h1 = h-dh;
h = h1;
u = v;
hobj.XData(end+1) = t;
hobj.YData(end+1) = h;
drawnow limitrate
end
But there's a graphics object called animatedline (introduced in R2014b) that is designed for exactly this job:
dt=1/1000;
g=9.81;
h=2;
u=0;
hobj = animatedline;
xlim([0 6])
ylim([-180 20])
for t=0:dt:6
v = u + (g*dt);
dh = 0.5*(u+v)*dt;
h1 = h-dh;
h = h1;
u = v;
addpoints(hobj,t,h)
drawnow limitrate
end
There's a lot more on this subject in this section of the documentation.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by