MATLAB 中的连续绘图 [英] Continuous plot in MATLAB

查看:105
本文介绍了MATLAB 中的连续绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个循环,我想绘制一些变量,

I have a loop in my code and I want to plot some variables,

在每次迭代中,我绘制一个新点,并希望将其连接到前一个点.

In each iteration I plot a new point and I want it to be connected to the previous point.

这是一个示例代码(在这段代码中,循环是不必要的,但在实际代码中,它不是).

Here is an example code (In this code the loop is unnecessary but in actual code, it's not).

n  = 500;
Fs = 1000;
f1 = 10;
t  = 0;
dt = 1 / Fs;
for i = 1 : n
    s = sin(2 * pi * f1 * t);
    t = t + dt;
    plot(t,s,'bo'); hold on;
    axis([0 t(end) -1 1]);
end

正确的就是我想要的.

我想使用 line 但这会变得混乱(我必须更改 i 并在每个 中使用 4 点行 命令).

I thought of using line but that would get messy (I would have to change i and use 4 points in each line command).

我似乎不是一个简单的问题,也许我遗漏了一些东西.

I't seems like a simple question, maybe I'm missing something.

感谢您的帮助.

推荐答案

这与@Nras 的答案相似,但更快更容易阅读.我有几个程序可以做这种事情,根据计算循环的时间长短,图形更新实际上可能是一个重要而恼人的瓶颈.

This is similar to the answer by @Nras, but is faster and easier to read. I've got several programs that do this sort of thing, and depending on how long your calculation loop is, the graphics update can actually be a significant and annoying bottleneck.

如果您知道您的向量最终会持续多长时间,您可以预先分配您的绘图,然后使用 handle 命令:

If you know how long your vector is ultimately going to be, you can pre-allocate your plot and then update using the handle command:

n  = 500;
Fs = 1000;
f1 = 10;
t  = 0;
dt = 1 / Fs;

s = nan(1,n);
emptyvec = nan(1,n);
h1 = plot(emptyvec,emptyvec,'-bo');
h2 = handle(line(emptyvec,emptyvec,'Color','r','Marker','x','LineStyle','--'));
h1 = handle(h1);


for i = 1 : n
    t = t + dt;
    s = sin(2 * pi * f1 * t);
    h1.XData(i) = t;
    h1.YData(i) = s;
    h2.XData(i) = t;
    h2.YData(i) = s^2;
    drawnow
end

MATLAB 在计算限制时会忽略 'nan',因此使用 nan 进行预分配使得对 xlim 的调用变得不必要.此外,当我运行它时 pause 是不必要的.你的旅费可能会改变.我个人更喜欢做这样的事情:

MATLAB ignores 'nan' when calculating the limits, so preallocating with nan makes the call to xlim unnecessary. Also, the pause is unnecessary when I run this. Your mileage may vary. I personally prefer to do something like this:

h = plot(t,emptyvec);

只要 t 是已知的并且不是很长,这不仅让我了解计算是如何进行的,而且还给了我一种进度条.如果它很长,我可能会在块中填充 t 或使用 XDataYData 作为循环缓冲区,使用 ii = mod(i,100); 作为索引(例如),会产生示波器类型的效果.此外,不断重新计算轴限制会产生时间损失.

As long as t is known and isn't crazy long, this gives me an idea of not only how the calculation is doing, but also gives me a sort of progress bar. If it is crazy long, I might populate t in chunks or use XData and YData as a circular buffer by using ii = mod(i,100); as the index (for instance), which will produce an oscilloscope type effect. Also, there is a time penalty for constantly recalculating the axis limits.

如果你不知道你的向量有多长(例如,如果它在一个 while 循环中),你可以预先分配一个你知道会更长的向量,分配块,或者只是做循环缓冲区的事情.

If you don't know how long your vector will be (if it's in a while loop, for instance), you can either preallocate with a vector that you know will be longer, allocate in chunks, or just do the circular buffer thing.

对于多个图,为每条附加线预分配使用 line 函数.与plot不同的是,您可以直接将更原始的line函数包装在handle函数中.

For multiple plots, preallocate use the line function for each additional line. Unlike plot, you can just wrap the more primitive line function in the handle function directly.

请注意,如果您使用的是 MATLAB 2014b 或更高版本(我不是),那么新的 HG2 图形系统使用对象作为句柄而不是双精度,因此 handle 命令将转换对象句柄的数字句柄是多余的,可以直接使用点表示法.另请注意,以这种方式使用的 handle未记录的

Note that if you're using MATLAB 2014b or greater (I'm not), then the new HG2 graphics system uses objects for handles rather than doubles, so the handle command, which converts the numeric handle to an object handle, is superfluous and the dot notation may be used directly. Also note that handle used this way is undocumented

这篇关于MATLAB 中的连续绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆