如何在MATLAB中从文本文件绘制实时数据 [英] How to plot real time data from text file in MATLAB

查看:491
本文介绍了如何在MATLAB中从文本文件绘制实时数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Matlab编码的新手,所以我仍然想尽一切办法. Interia传感器几乎不起作用,该传感器每10ms输出一次传感器方向数据.我无法将这些数据存储到一个不断更新的文本文件中.

Im new to matlab coding so im still trying to get my head around things. Pretty much im working with interia sensors which output the sensors orientation data every 10ms. Im able to store this data into a text file which is continuously updating.

我现在的任务是实时绘制这些数据.这涉及连续访问和读取文本文件(如果可能,每10ms)并根据时间绘制此数据的图形. 你们中的任何一个可以给我一些指导,以指导最有效的方法是什么.

My task now is to plot this data in real time. This involves continuously accessing and reading from the text file (every 10ms if possible) and graph this data with respect to time. Can any of you guys give me some guidance onto what would be the most effective way to do this.

此时,文本文件仅存储有关一个参数(传感器的x坐标)的数据.我可以通过2种方式存储此数据: 方法1:每10毫秒更新一次数据.每个都存储在一个新行中. 方法2:我可以使文本文件仅包含最新数据(擦除以前的数据) 我可以使用这两种方法中的任何一种..你们觉得什么都更容易.

At this moment in time, the text file only stores data about one parameter (the x coordinate of the sensor). I can store this data in 2 way: Method 1: New data every 10ms. Each is stored in a new line. Method 2: I can make the text file only have the most recent piece of data (erasing previous data) Im able to use either of these methods.. whatever you guys think would be easier.

我曾经尝试使用其他第三方软件从文本文件中绘制出这些数据的图形,但它们似乎都非常跳跃,无法足够快地从文本文件中读取数据.

Ive tried using other 3rd party software to graph this data from the text file, but they all seemed really jumpy and couldnt read from the text file fast enough.

谢谢.

推荐答案

MATLAB计时器对象会有所帮助.例如,请参见此链接

A MATLAB timer object will help. See, for example, this link Using MATLAB to process files in real-time after every instance a file is created by a separate program.

对于同时写入(您的其他过程)和读取(MATLAB)到单个文件可能会有些担心.您的情况可能更适合管道而不是文件:管道与临时文件,但是我将继续使用基于MATLAB的解决方案来进行定时文件读取和绘图更新.

There may be some worry regarding the simultaneous write (your other process) and read (MATLAB) to a single file. Your situation may be more suited to a pipe rather than a file: Pipe vs. Temporary File but I will proceed with a MATLAB based solution for timed file reads and plot updates.

我模仿了你的情况,并学到了一些东西:

I emulated your situation and learned a few things:

  • MATLAB文件句柄存储当前位置.但是,如果为文件ID设置了文件结尾标志,则在随后调用fread时,MATLAB将不会查找新数据.请参见下面的readFile函数中的使用fseek的变通方法.
  • 10毫秒是乐观的. MATLAB不会遗漏点,但是每次更新通常会添加四个或更多数据点,而不仅仅是一个.
  • 如果数据源使用的是缓冲文件写入,则在将缓冲区刷新到磁盘之前,MATLAB不会看到新数据.以下python代码用于模拟数据源.打开该文件以写入而没有缓冲:fobj = open('test.dat', 'wb', 0).
  • The MATLAB file handle stores the current position. However if the end of file flag is set for a file-id MATLAB will not look for new data when a subsequent call to fread is made. See the work arounds using fseek in the readFile function below.
  • 10 ms is optimistic. MATLAB doesn't miss points but each update typically adds four or more data points rather than just one.
  • If the source of the data is using buffered file writes MATLAB won't see new data until the buffer is flushed to disk. The python code below was used to emulate the source of your data. The file was opened to write without buffering: fobj = open('test.dat', 'wb', 0).

下面的MATLAB代码:

MATLAB code below:

function [t] = livePlot(period, filename)
    //%   inputs : period : update rate in seconds
    //%            filename : name of the file to get data from
    //%
    //%   outputs: t      : the timer object
    //%                     >> stop(t) 
    //%                     ends streaming
    //%%

    close all;        
    t = timer('StartDelay', 1, 'Period', period, ...
              'ExecutionMode', 'fixedRate');
    //%% timer object callback functions
    t.StopFcn  = {@stopFigure};
    t.TimerFcn = {@updateFigure};
    //%% initialize timer object user data
    d = get(t, 'UserData');
    d.data = []; % array for the data to plot
    axes('Position', [0 0 1 1], 'Visible', 'off');
    d.axes_handle = axes('Position', [.2 .1 .7 .8]);
    d.line_handle = plot(NaN,NaN);
    d.fid = fopen(filename, 'r');    
    set(t, 'UserData', d);    
    start(t);
end

function stopFigure(obj, event)
    //% close function handle
    d = get(obj, 'UserData');
    fclose(d.fid);
end

function updateFigure(obj, event)
    d = get(obj, 'UserData');        
    //% read new data from file
    tmp = readFile(obj);
    //% append to array in user data
    d.data = [d.data transpose(tmp)];
    //% update the plot 
    set(gcf, 'CurrentAxes', d.axes_handle);
    set(d.line_handle, 'XData', 1:length(d.data), 'YData', d.data);
    //% store the timer object user-data
    set(obj, 'UserData', d);
end

function [tmp] = readFile(obj)
    //% read binary data. file-ID is in the timer user-data
    d = get(obj, 'UserData');
    tmp = fread(d.fid);
    fprintf('Current file location : %d \n', ftell(d.fid));
    //% fprintf('End of file indicator : %d \n', feof(d.fid));
    //% reset the end-of-file indicator
    fseek(d.fid, -1, 0);
    fseek(d.fid, 1, 0);
    //% fprintf('End of file indicator : %d \n', feof(d.fid));
    set(obj, 'UserData', d); 
end

每10毫秒将Python数据写入文件的Python代码:

Python code to write data to a file every ~ 10 milliseconds:

#!/anaconda/bin/python
import numpy as np
from time import sleep
sleep_time = 0.01
sigma = 5
fobj = open('test.dat', 'wb', 0)
for i in range(5000):
    sleep(sleep_time)
    x = int(np.random.normal(sigma))
    fobj.write('%c' % x)
fobj.close()

这篇关于如何在MATLAB中从文本文件绘制实时数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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