在matlab中使用txt和date循环文件名 [英] looping file names with txt and date in matlab

查看:177
本文介绍了在matlab中使用txt和date循环文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数千个日常数据文件中提取数据,如下所示。根据以下文件名的日期,我如何将它们放在循环中? .7在文件名末尾都有所有文件。
3B42_daily.1999.02.27.7.nc,3B42_daily.1999.02.28.7.nc,3B42_daily.1999.02.27.7.nc,so ... .....

解决方案

我建议使用regexp()分割和分组这些信息,让我们从你所在的列表中创建一个。当然,您应该通过dir()获取此列表,如果一段工作代码,但现在,我们从$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ = {'3B42_daily.1999.02.27.7.nc','3B42_daily.1999.02.28.7.nc','3B42_daily.1999.02.27.7.nc'}

然后你可以使一个单元格结构将它们分离出来像这样

  names = regexp(文件'\w +。(?< year> [0-9] {4})(?< month> [0-9] {2})(?< day> [0- 9] {2})。\w +','names')

每个条目将包含年,月和日分隔的单元格数组。您可以访问元素,如

 名称{1} .day 
名称{2} .day

希望有所帮助。


I want to extract data from thousands of daily data files as shown below. How would I put them in a loop based on date with following file names? .7 in the end of file name is there with all files. 3B42_daily.1999.02.27.7.nc, 3B42_daily.1999.02.28.7.nc, 3B42_daily.1999.02.27.7.nc, so on.....

解决方案

I suggest using regexp() to split and group those information, let's make one from the list you have there. Of course, you should get this list through dir() if a piece of working code but for now, let's start with

files = {'3B42_daily.1999.02.27.7.nc', '3B42_daily.1999.02.28.7.nc', '3B42_daily.1999.02.27.7.nc'}

Then you can make a cell structure to separate them out like so

names = regexp(files, '\w+.(?<year>[0-9]{4}).(?<month>[0-9]{2}).(?<day>[0-9]{2}).\w+', 'names')

Now, names should be a cell array where each entry would contain year, month and day separated. You can access the elements like

names{1}.day
names{2}.day

Hope that helps.

这篇关于在matlab中使用txt和date循环文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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