从文本文件中读取和绘图 [英] Reading and Plotting from text files

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

问题描述

文本文件中包含

  Date;时间;温度°C 
05.08.2011; 11:00:47; 23.75
05.08.2011; 11:01:21; 23.69
05.08.2011; 11:01:56; 25.69
05.08.2011; 11:02:16; 23.63
05.08.2011; 11:02:50; 23.63
05.08.2011; 11:03:24; 23.63

我想绘制经过分钟的温度值。
首先我使用了

pre $ text $('file1.txt','%s%s ','headerlines',1)

读取字符串中的数据,我得到

  '17:09:16; 21.75'

之后我使用了

lockquote
blockquote
a = strread('17:08 :00; 21.81','%s','delimiter',';')


得到

  '17:08:00'
'21 .81'

但是在此之后,我无法弄清楚如何处理这两个字符串,尤其是时间。
我想在xaxis上绘制温度随时间的变化。但是这次不是这个时间,在这种情况下是2分37秒。
需要帮助

感谢Aabaz.thats真的是一大利益..我知道为什么我可以弄明白..我花了这么多的时间在
我有一些包含这个数据的50个文件。如果我想循环下这个代码,怎么能做到这一点,因为我有我的名字下的ROM IDs..alike 1AHJDDHUD1224.txt文件。
如何在循环中传递文件名。是否必须更改文件的名称,然后在循环中传递它们。我呃knw

我有还有一个问题是,如果我希望在每60秒之后绘制值,则尽快在文本文件中绘制数据图形,然后每60秒更新一次图形,直到文本文件中有更多的值可用

解决方案

考虑下面的代码。它会循环访问特定目录中的所有.DAT文件,读取数据文件,然后用格式化为日期/时间的x轴进行绘图:

 %#获取文件列表
BASE_DIR ='C:\Users\Amro\Desktop';
files = dir(fullfile(BASE_DIR,'*。dat'));
files = {files.name};

%#首先读取所有文件
dt = cell(numel(files),1);
temps = cell(numel(files),1);
for i = 1:numel(files)
%#读取数据文件
fname = fullfile(BASE_DIR,files {i});
fid = fopen(fname);
C = textscan(fid,'%s%s%f','delimiter',';','HeaderLines',1);
fclose(fid);

#datetime和temperature
dt {i} = datenum(strcat(C {1},{''},C {2}));
temps {i} = C {3};
end




$现在我们可以绘制数据(比如我们有16个文件, (4,4)

  figure 
for i = 1:16
subplot(4, ('Temp')
datetick('x' ,'HH:MM:SS')
end


I have some problems in reading data from text file and plotting it.The text file contains

Date; Time; Temp °C
05.08.2011; 11:00:47;23.75
05.08.2011; 11:01:21;23.69
05.08.2011; 11:01:56;25.69
05.08.2011; 11:02:16;23.63
05.08.2011; 11:02:50;23.63
05.08.2011; 11:03:24;23.63

I want to plot the Temperature values with elapsed minutes. firstly i used

[a,b]=textread('file1.txt','%s %s','headerlines',1)

to read the data in a string and I get

'17:09:16;21.75'

After that I used

a= strread('17:08:00;21.81','%s','delimiter', ';')

to get

'17:08:00'
'21.81'

But after this I am not been able to figure out how to move forward to deal with both these strings, especially time. I want to plot temperature with time on xaxis..but not this time the elapsed time..in this case 2 mins 37 secs. Help needed

Thanks Aabaz.thats really a big favor..I dun why I could figure it out ..I spent so much time on it I have some 50 files comprising this data..If i want to loop it under this code , how can accomplish it, cz i have names of the file under ROM IDs..alike 1AHJDDHUD1224.txt. How wud pass the file names in the loop.Do I have to change the names of the files then pass them under loop.I dun knw

I have one more question that if I wanted the values to be plotted after every 60 seconds..alike as soon the data is available in text files graph is plotted , and then graph is updated after every 60 sec until some more values are available in text file

解决方案

Consider the following code. It will cycle through all .DAT files in a specific directory, read the data files, then plots with a the x-axis formatted as date/time:

%# get a list of files
BASE_DIR = 'C:\Users\Amro\Desktop';
files = dir( fullfile(BASE_DIR,'*.dat') );
files = {files.name};

%# read all files first
dt = cell(numel(files),1);
temps = cell(numel(files),1);
for i=1:numel(files)
    %# read data file
    fname = fullfile(BASE_DIR,files{i});
    fid = fopen(fname);
    C = textscan(fid, '%s %s %f', 'delimiter',';', 'HeaderLines',1);
    fclose(fid);

    %# datetime and temperature
    dt{i} = datenum( strcat(C{1},{' '},C{2}) );
    temps{i} = C{3};
end

Now we can plot the data (say we had 16 files, thus layout subplots as 4-by-4)

figure
for i=1:16
    subplot(4,4,i), plot(dt{i}, temps{i}, '.-')
    xlabel('DateTime'), ylabel('Temp °C')
    datetick('x','HH:MM:SS')
end

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

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