【问题标题】:Matlab: Read .txt file in matlab and save dateMatlab:在matlab中读取.txt文件并保存日期
【发布时间】:2016-05-24 12:24:17
【问题描述】:

我有一个包含这些数据的 .txt 文件:

user check-in time (year-month-day hours) latitude  longitude  location id
  0 2010-10-03  22-21-49         30.2448597206  -97.7571630478  18417
  0 2010-10-02  01-48-43         30.2558143993  -97.7634179592  4256132
  0 2010-10-01  14-36-39         30.2679095833  -97.7493124167  21714
  0 2010-09-30  23-23-22         30.2643362697  -97.7410461251  211286
  0 2010-09-29  00-31-24         30.2379105     -97.79996073    539065
  0 2010-09-28  23-47-07         30.2313745667  -97.79745475    15590
  0 2010-09-27  14-52-01         30.2691029532  -97.7493953705  420315
  0 2010-09-26  16-43-45         30.250617981   -97.7658996582  121955
  0 2010-08-30  00-11-01         30.2509939667  -97.7489662167  9508
  0 2011-08-28  22-27-57         30.2448597206  -97.7571630478  18417

我想在 matlab 中读取这个文件,我想在变量 A 中只保存 2010 年和第 10 个月和第 8 个月的行,你能帮帮我吗?

要阅读 .txt,我使用了以下代码:

fileID = fopen('text.txt');
C = textscan(fileID,'%d %d %d %d %d %d %d %f %f %f');
fclose(fileID);

但是在我不知道如何选择我想要的行之后。

【问题讨论】:

  • 如果它解决了您的问题,请不要忘记点赞

标签: matlab


【解决方案1】:

使用此代码读取每一行并比较每行的前 6 个字符(如果它们与 2010 年和第 10 个月匹配),它将行存储在“保存”单元格数组中

    fileID = fopen('test2.txt');
C = textscan(fileID,'%s %s %s %s %s %s %s %s %s %s','Delimiter',',')
fileID2 = fopen('test2.txt');
C2 = textscan(fileID2,'%s %s %s %s %s %s %s %s %s %s')
for i = 1:11
   if(strncmp([C2{1}{i} ' 2010-10'],C{1}{i},9))
       save{i,:} = C{1}{i}
   end
end

这是输出

save = 

    []
    '0 2010-10-03  22-21-49         30.2448597206  -97.7571630478  18417'
    '0 2010-10-02  01-48-43         30.2558143993  -97.7634179592  4256132'
    '0 2010-10-01  14-36-39         30.2679095833  -97.7493124167  21714'

出于测试目的,我创建了一个新的文本文件,其中包含更多 2011 而不是 2010

user check-in time (year-month-day hours) latitude  longitude  location id

  0 2010-10-03  22-21-49         30.2448597206  -97.7571630478  18417
  0 2010-10-02  01-48-43         30.2558143993  -97.7634179592  4256132
  0 2010-10-01  14-36-39         30.2679095833  -97.7493124167  21714
  0 2010-09-30  23-23-22         30.2643362697  -97.7410461251  211286
  0 2010-09-29  00-31-24         30.2379105     -97.79996073    539065
  0 2010-09-28  23-47-07         30.2313745667  -97.79745475    15590
  0 2011-09-27  14-52-01         30.2691029532  -97.7493953705  420315
  0 2011-09-26  16-43-45         30.250617981   -97.7658996582  121955
  0 2011-08-30  00-11-01         30.2509939667  -97.7489662167  9508
  0 2011-08-28  22-27-57         30.2448597206  -97.7571630478  18417

输出根据文件变化

save = 

    []
    '0 2010-10-03  22-21-49         30.2448597206  -97.7571630478  18417'
    '0 2010-10-02  01-48-43         30.2558143993  -97.7634179592  4256132'
    '0 2010-10-01  14-36-39         30.2679095833  -97.7493124167  21714'

你也可以访问每一行

save{2,1}

ans =

0 2010-10-03  22-21-49         30.2448597206  -97.7571630478  18417

【讨论】:

  • 谢谢,如何选择 2010 年和 10 月的行?我必须修改这行代码:'if(strncmp([C2{1}{i}'2010'],C{1}{i},6))'
  • @elis56 尝试更新后的代码,它适用于第 10 个月,您只需在 strncmp() 中添加 '2010-10' 和 9 而不是 6 个字符
猜你喜欢
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2018-04-12
  • 2012-03-19
  • 2012-01-31
相关资源
最近更新 更多