在 MATLAB 中从 CSV 文件读取文本数据

发布于 2024-11-25 05:29:10 字数 353 浏览 7 评论 0原文

我的数据采用以下形式:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

用心笑 2024-12-02 05:29:10

以下是我读取制表符分隔值并解析日期的方法:

%# 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')

在此处输入图像描述

至于星期几列,您可以将其获取为:

>> 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'

这会产生不同的日期(您发布的数据只是编造的,或者您在生成这些日期的部分中存在错误!)

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')

enter image description here

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!)

古镇旧梦 2024-12-02 05:29:10

根据上一个问题的提示,

fid = fopen('filename.txt');
% Skip a line for the header
s = fgetl(fid);
% Read the rest into data
data = textscan(fid, '%s %s %d %d %d %d');
% Close the file
fclose(fid);

一周中的日子位于数据的第一个单元格中。

Taking prompts from this previous question,

fid = fopen('filename.txt');
% Skip a line for the header
s = fgetl(fid);
% Read the rest into data
data = textscan(fid, '%s %s %d %d %d %d');
% Close the file
fclose(fid);

The days of the week are in the first cell of data.

怂人 2024-12-02 05:29:10

您可以从文件交换下载我的 csvimport 提交内容。假设您的数据是制表符分隔的,您可以使用以下方式读取它:

[days datecol timecol visitors] = csvimport( 'file.txt', 'delimiter', '\t', ...
       'columns', {'days of week', 'date', 'time(hrs)', 'visitors'} );

前 2 个输出参数将是字符串元胞数组,而后 2 个将是双精度矩阵。

You could download my csvimport submission from the File Exchange. Assuming your data is tab separated, you can read it using:

[days datecol timecol visitors] = csvimport( 'file.txt', 'delimiter', '\t', ...
       'columns', {'days of week', 'date', 'time(hrs)', 'visitors'} );

The first 2 output parameters will cell arrays of strings while the last 2 will be double matrices.

生生漫 2024-12-02 05:29:10

如果您刚刚开始使用 matlab(最新版本),最简单的方法是使用“导入向导”。

几个简单的步骤:

  1. 浏览到您的文件并右键单击它
  2. 选择要导入的选项
  3. 选择将内容存储为元胞数组的选项(向量或矩阵不起作用)。
  4. 单击“导入”

您也可以单击“下一步”进行导入,并选择要为此过程生成代码。然而,这可能会有点冗长。如果你只需要做一次,我会推荐这种方法。

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:

  1. Browse to your file and right click it
  2. Choose the option to import
  3. Select the choice to store things as cell array (Vectors or matrices wont work).
  4. Click Import

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.

Bonjour°[大白 2024-12-02 05:29:10

您可以尝试使用dlmread。它可以采用任何 ASCII 分隔符。我认为它可能适合您的要求。请参阅此处

You could try to use dlmread. It can take any ASCII delimiter. I think it might suit your requirements. See here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文