haibaraxx的个人博客分享 http://blog.sciencenet.cn/u/haibaraxx

博文

Matlab: date&time 日期与时间信息提取与转换

已有 13767 次阅读 2017-1-20 21:31 |个人分类:Matlab|系统分类:科研笔记| time, Date

描述:现有文件acs_05_230715_1811.dat,第1行包含日期与时间信息,内部结构如下:


问题:将日期与时间提取出来,保存成变量,并进行类型转换。

脚本:

------------------------------------------------------------------------------------------------------------

脚本1

fid=fopen('acs_05_230715_1811.dat','r');

line=fgetl(fid);  % get the first line
line(find(line=='-'))='/'; % 将-替换成/

DateStr=line(17:35);
DateNum=datenum(DateStr,'mm/dd/yyyy HH:MM:SS'); %same with: DateNum=datenum(DateStr,0);
DateVec=datevec(DateStr,'mm/dd/yyyy HH:MM:SS'); % same with: DateVec=datevec(DateStr,0);

fclose(fid);

-------------------------------------------------------

脚本2

fid=fopen('acs_05_230715_1811.dat','r')

line=fgetl(fid);  % get the first line

DateStr=line(17:35);
DateNum=datenum(DateStr,'mm-dd-yyyy HH:MM:SS');
DateVec=datevec(DateStr,'mm-dd-yyyy HH:MM:SS');

fclose(fid);

------------------------------------------------------------------------------------------------------------

脚本文件:

DateTime.txt



% 运行到第5行:变量line为char型,内容为acs_05_230715_1811.dat的第1行.

% 运行到第6行:变量line为char型,内容为将acs_05_230715_1811.dat第1行的"-"替换成"/".

% 运行到第8行:变量DateStr为char型.


% 运行到第9行:变量DateNum转化为时间的数值格式,double型.

% 运行到第10行:变量DateVec转化为时间的向量格式,double型.




https://wap.sciencenet.cn/blog-3031432-1028911.html

上一篇:建议科学网博客解除可上传的附件的格式要求
下一篇:Matlab:跳过文件的前几行
收藏 IP: 134.1.1.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-10 06:41

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部