惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - febird

改进 GAE cpedialog 博客程序的一些细节 Python 格式化输出时间字符串函数 strftime 从IEEE802.1到IEEE802.22 CodeWarrior for Freescale S12 开发Tips - febird 第一个 Android 程序 [资料] IPv6主机地址 Web中使用MSCOMM32.OCX读写串口(2) Web中使用MSCOMM32.OCX读写串口 Matlab中Timer的使用 Matlab多窗口传值问题(GUI) My Matlab Tips (Unfinished) Matlab GUIDE使用总结--Matlab GUI界面 Matlab Deploy工具的使用--Matlab生成可执行文件 使用matlab作为图表绘制工具--Matlab使用入门 基于DE2 与Nios II的音频录放系统的设计-软件篇 基于DE2 与Nios II的音频录放系统的设计-系统篇 GMail今天更换外观了-支持主题 Google WebMaster网站验证错误(404返回成功)解决方法 Febird.co.cc 恢复正常访问
Matlab中使用Plot函数动态画图方法总结
febird · 2008-12-02 · via 博客园 - febird

  Matlab除了强大的矩阵运算,仿真分析外,绘图功能也是相当的强大,静态画图没什么问题,由于Matlab本身的多线程编程缺陷,想要动态的画图,并且能够很好的在GUI中得到控制,还不是一件很容易的事情,下面总结几种方法。

  一. AXIS 移动坐标系
    这种方法是最简单的一种方法,适合于数据已经全部生成的场合,先画图,然后移动坐标轴。实例代码如下:

%%
%先画好,然后更改坐标系
%在命令行中 使用 Ctrl+C 结束
t=0:0.1:100*pi;
m=sin(t);
plot(t,m);
x=-2*pi;
axis([x,x+4*pi,-2,2]);
grid on
while 1
    if x>max(t)
        break;
    end
    x=x+0.1;
    axis([x,x+4*pi,-2,2]); %移动坐标系
    pause(0.1);
end

 二. Hold On 模式
    
       此种方法比较原始,适合于即时数据,原理是先画上一帧,接着保留原始图像,追加下一幀图像,此种方式比较繁琐,涉及画图细节,并且没有完整并连续的Line对象数据。

   例如:

%%
% Hold On 法
% 此种方法只能点,或者

分段划线
hold off
t=0;
m=0;
t1=[0 0.1]; %要构成序列
m1=[sin(t1);cos(t1)];
p = plot(t,m,'*',t1,m1(1,:),'-r',t1,m1(2,:),'-b','MarkerSize',5);  
x=-1.5*pi;
axis([x x+2*pi -1.5 1.5]);
grid on;

for i=1:100
    hold on
    t=0.1*i;  %下一个点
    m=t-floor(t);
    t1=t1+0.1; %下一段线(组)
    m1=[sin(t1);cos(t1)];
    p = plot(t,m,'*',t1,m1(1,:),'-r',t1,m1(2,:),'-b','MarkerSize',5);  
    x=x+0.1;
    axis([x x+2*pi -1.5 1.5]);
    pause(0.01);
end

三.  Plot 背景擦除模式

  这种模式比较适合画动画,效率比较高,刷新闪烁小,适合即时数据,最终的Line结构数据完整。

了解此方法之前要搞清楚 Plot函数的原型是什么: Plot函数,输入为 X-Y (-X)坐标元组、以及“属性”-“值对,输出为一个列向量(每条曲线岁对应的Line结构 Handle,每一行代表一个 线条的handles), 每一线条都有 XData,YData 向量。如果你画了2条线,那么会返回 2×1的向量。
  重新画图不需要 重新书写 Plot,只需要 刷新图像即可,使用drawnow函数。

完整实例如下:

1. 画一个点的动画:

%%
%采用背景擦除的方法,动态的划点,并且动态改变坐标系
% t,m 均为一行 ,并且不能为多行
t=0;
m=0;
p = plot(t,m,'*',...
   'EraseMode','background','MarkerSize',5);
x=-1.5*pi;
axis([x x+2*pi -1.5 1.5]);
grid on;

for i=1:1000
    t=0.1*i;       %两个变量均不追加 
    m=sin(0.1*i);
    set(p,'XData',t,'YData',m)
    x=x+0.1;   
    drawnow
    axis([x x+2*pi -1.5 1.5]);
    pause(0.1);
end

2. 动态多条曲线(即时数据)
 
  %%
%采用背景擦除的方法,动态的划线,并且动态改变坐标系
% 多行划线

t=[0]
m=[sin(t);cos(t)]
p = plot(t,m,...
   'EraseMode','background','MarkerSize',5);
x=-1.5*pi;
axis([x x+2*pi -1.5 1.5]);
grid on;

for i=1:1000
    t=[t 0.1*i];                   %Matrix 1*(i+1)
    m=[m [sin(0.1*i);cos(0.1*i)]]; %Matrix 2*(i+1)
    set(p(1),'XData',t,'YData',m(1,:))
    set(p(2),'XData',t,'YData',m(2,:))   
    drawnow
    x=x+0.1;   
    axis([x x+2*pi -1.5 1.5]);
    pause(0.5);
end

上面的这几个画图方式的示例只是简单的for循环,是单线程的,如果是涉及到GUI的编程,那么请使用Timer来完成这件事情,Timer是我在Matlab中实现多线程唯一方法(没有找到别的方法)。