连续绘图在MATLAB [英] Continuous plot in MATLAB

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

问题描述

我在code一环,我想绘制一些变量,

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

在每次迭代我绘制一个新的起点,我希望它可以连接到previous点。

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

下面是一个例子code(在此code环路是不必要的,但在实际的code,它不是)。

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

那个合适的人是我想要的。

The right one is what I want.

我觉得的使用,但将变得混乱(我将不得不改变和使用 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似乎是一个简单的问题,也许我失去了一些东西。

I't seems 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.

如果您知道过了多久你的载体最终将是,你可以pre-分配你的情节,然后更新使用的 处理 命令:

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忽略'南'计算的极限,所以preallocating与在做调用 XLIM 不必要的。此外,暂停是不必要的,当我运行此。你的情况可能会有所不同。我个人preFER做这样的事情:

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 块中,或使用扩展数据 YDATA 作为循环缓冲区使用 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.

如果你不知道你的载体有多长(如果它是在,而循环,例如),您可以preallocate用向量你知道会比较长,分配的块,或者只是做循环缓冲区的事情。

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.

有关多条曲线,preallocate使用功能,每增加一行。不同于剧情,你可以包裹在处理功能C $ C>函数直接。

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图形系统使用对象的句柄,而不是双打,因此处理命令,其中数字手柄转换为一个对象柄,是多余的点符号可直接使用。还要注意的是处理用这种方式无证

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天全站免登陆