在 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
全年的每一天也是如此。 我需要创建一周中的天数矩阵,如下所示:
A=[
mon
mon
mon
]
my data is in following form:
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
likewise for every day for entire year.
i need to create a matrix of days of week as shown below:
A=[
mon
mon
mon
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是我读取制表符分隔值并解析日期的方法:
至于星期几列,您可以将其获取为:
这会产生不同的日期(您发布的数据只是编造的,或者您在生成这些日期的部分中存在错误!)
Here is how I would read the tab-separated values, and parse the dates:
As for the day-of-week column, you can get it as:
this produces different days (either your data posted was simply made-up, or you have a bug in the part that generated those dates!)
根据上一个问题的提示,
一周中的日子位于
数据
的第一个单元格中。Taking prompts from this previous question,
The days of the week are in the first cell of
data
.您可以从文件交换下载我的 csvimport 提交内容。假设您的数据是制表符分隔的,您可以使用以下方式读取它:
前 2 个输出参数将是字符串元胞数组,而后 2 个将是双精度矩阵。
You could download my csvimport submission from the File Exchange. Assuming your data is tab separated, you can read it using:
The first 2 output parameters will cell arrays of strings while the last 2 will be double matrices.
如果您刚刚开始使用 matlab(最新版本),最简单的方法是使用“导入向导”。
几个简单的步骤:
元胞数组
的选项(向量或矩阵不起作用)。您也可以单击“下一步”进行导入,并选择要为此过程生成代码。然而,这可能会有点冗长。如果你只需要做一次,我会推荐这种方法。
If you are just getting started with (a recent version of) matlab, the easiest way is to use the 'import wizard'.
A few simple steps:
cell array
(Vectors or matrices wont work).Optionally you can click next to import and select that you want to generate the code for this procedure. However, this will likely be a bit verbose. If you just need to do it once, I would recommend this method.
您可以尝试使用
dlmread
。它可以采用任何 ASCII 分隔符。我认为它可能适合您的要求。请参阅此处。You could try to use
dlmread
. It can take any ASCII delimiter. I think it might suit your requirements. See here.