在MATLAB中从CSV文件读取文本数据 [英] Reading text data from a CSV file in MATLAB
本文介绍了在MATLAB中从CSV文件读取文本数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的资料格式如下:
days of week date time(hrs) visitors
mon jan 2 2010 900 501
mon jan 2 2010 1000 449
mon jan 2 2010 1100 612
同样对于整年的每一天。
i需要创建一个星期几的矩阵,如下所示:
likewise for every day for entire year. i need to create a matrix of days of week as shown below:
A=[
mon
mon
mon
]
推荐答案
以下是我阅读标签分隔值的方法,并解析日期:
Here is how I would read the tab-separated values, and parse the dates:
%# read and parse file
fid = fopen('data.csv','rt');
C = textscan(fid, '%s %s %s %d', 'Delimiter','\t', 'HeaderLines',1, ...
'MultipleDelimsAsOne',true, 'CollectOutput',false);
fclose(fid);
%# get date and number of visitors
dt = datenum(strcat(C{2}, {' '}, C{3}), 'mmm dd yyyy HHMM');
visitors = C{4};
%# plot
plot(dt,visitors)
datetick('x')
xlabel('time of day'), ylabel('visitors')
对于周日列,您可以将其作为:
As for the day-of-week column, you can get it as:
>> C{1} %# first column from file
ans =
'mon'
'mon'
'mon'
>> cellstr(datestr(dt,'ddd')) %# actual day of week from parsed dates
ans =
'Sat'
'Sat'
'Sat'
这会产生不同的日期(您发布的数据只是补充,或者您的部分有错误生成这些日期!)
this produces different days (either your data posted was simply made-up, or you have a bug in the part that generated those dates!)
这篇关于在MATLAB中从CSV文件读取文本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文