matlab如何读取日期变量?

matlab如何读取日期变量?,第1张

最基本的tic,toc
tstart = tic;
elapsed = toc(tstart); %计算的是从tstart开始到toc的时间间隔
运行结果示例:
tstart = tic
tstart =
373853070750740
elapsed = toc(tstart)
elapsed =
811367
2、时间变量以及文件读取
help textscan
TEXTSCAN Read formatted data from text file or string
C = TEXTSCAN(FID,'FORMAT') reads data from an open text file identified
by FID into cell array C Use FOPEN to open the file and obtain FID
The FORMAT is a string of conversion specifiers enclosed in single
quotation marks The number of specifiers determines the number of
cells in the cell array C For more information, see "Format Options"

C = TEXTSCAN(FID,'FORMAT',N) reads data from the file, using the FORMAT
N times, where N is a positive integer To read additional data from
the file after N cycles, call TEXTSCAN again using the original FID

C = TEXTSCAN(FID,'FORMAT','PARAM',VALUE) accepts one or more
comma-separated parameter name/value pairs For a list of parameters
and values, see "Parameter Options"

C = TEXTSCAN(FID,'FORMAT',N,'PARAM',VALUE) reads data from the
file, using the FORMAT N times, and using settings specified by pairs
of PARAM/VALUE arguments

C = TEXTSCAN(STR,) reads data from string STR You can use the
FORMAT, N, and PARAM/VALUE arguments described above with this syntax
However, for strings, repeated calls to TEXTSCAN restart the scan from
the beginning each time (To restart a scan from the last position,
request a POSITION output See also Example 3)

[C, POSITION] = TEXTSCAN() returns the file or string position at
the end of the scan as the second output argument For a file, this is
the value that FTELL(FID) would return after calling TEXTSCAN For a
string, POSITION indicates how many characters TEXTSCAN read

Notes:

When TEXTSCAN reads a specified file or string, it attempts to match
the data to the format string If TEXTSCAN fails to convert a data
field, it stops reading and returns all fields read before the failure

Format Options:

The FORMAT string is of the form: %<WIDTH><PREC><SPECIFIER>
<SPECIFIER> is required; <WIDTH> and <PREC> are optional
<WIDTH> is the number of characters or digits to read
<PREC> applies only to the family of %f specifiers, and specifies
the number of digits to read to the right of the decimal point

Supported values for SPECIFIER:

Numeric Input Type Specifier Output Class
------------------ --------- ------------
Integer, signed %d int32
%d8 int8
%d16 int16
%d32 int32
%d64 int64
Integer, unsigned %u uint32
%u8 uint8
%u16 uint16
%u32 uint32
%u64 uint64
Floating-point number %f double
%f32 single
%f64 double
%n double

TEXTSCAN converts numeric fields to the specified output type
according to MATLAB rules regarding overflow, truncation, and the
use of NaN, Inf, and -Inf For example, MATLAB represents an
integer NaN as zero

TEXTSCAN imports any complex number as a whole into a complex
numeric field, converting the real and imaginary parts to the
specified type (such as %d or %f) Do not include embedded white
space in a complex number

Character Strings Specifier Details
----------------- --------- -------------------------

在计时开始点加一个tic
在计时结束点加一个toc
即可。
例如:
tic
m = magic(100);
toc
结果:
Elapsed time is 0102184 seconds

这样计算

clc;clear
t1 ='2018-08-04 02:29:30';
t2 = '2018-08-04 02:29:32';
t1v = datevec(t1);
t2v = datevec(t2);
t1 = calyears(t1v(1))+calmonths(t1v(2)) + caldays(t1v(3)) + duration(t1v(4:6));
t2 = calyears(t2v(1))+calmonths(t2v(2)) + caldays(t2v(3)) + duration(t2v(4:6));
dt = t2-t1答案

dt = 
   0h 0m 2s

设置时间轴
ts=datenum('1999-01-01 00:00:00');% 开始时间
tf=datenum('1999-01-01 00:20:00');% 结束时间
y=rand(21,1);% 给出你的y值,我这里随机给了
t=linspace(ts,tf,21);% 21min为总共的时间
plot(t,y);
% 调用datetick函数直接生成时间坐标
datetick('x','HH:MM','keepticks')
使用matlab也可以很方便的绘制时间序列,但是对于时间轴的设置却比较困难。例如minor ticks的设置和标签位置剧中要求的设置。因此使用GMT绘制时间序列。主要是为了方便次级月标签的绘制。
第一节:时间序列
1绘制时间序列,手动调整坐标刻度,一年为一个主刻度(main ticks),一月为一个次刻度(minor ticks),并且绘制Y坐标的grid。
具体步骤如下:
1首先是准备好规则数据。如果数据格式是matlab处理和存储的矩阵格式,使用MATLAB则需要提取出来时间序列,并保存为ASCII格式数据。注意此时的数据不带有时间信息,只是简单的一列数据而已,我们将在在后面添加时间信息。
2为sigma0数据添加时间列[h2]
这个方法不是固定的,可以自己编程实现,也可以使用已有的简单程序。注意时间数据的格式是固定的几种。
如果数据不多的话,可以使用Windows自带的excel添加。
本例子的最终数据格式如下:第一列是时间,第二列到第四列是sigma0的属性值,(time,sigma0Ku,sigma0C,sigma0C-Ku)。
3 GMT绘图
将三列sigma0的数据放到一幅图上,即同时绘制三条时间序列,且使用同一个坐标系。横坐标为年月,具体设置为:年为大刻度,月为小刻度,界限设置为199311-2004121。竖坐标为sigma0的值,范围可当已知,设置为0-15。
本例子参考GMT Cookbook中的程序21:
GMT中Time格式需要按照数据的时间格式设置。在GMT的帮助文件中可以找到,有格里高利,ISO之类。还要区分输入的时间格式和输出的时间格式。
我们选择例21中使用的时间标准dd-o-yy。(GMT有细致的时间设置参数,注意在这一步别出错误。)
注意:数据文件中时间序列的日期坐标可以和标注的日期坐标格式不一样,但是时间上要一致。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13041999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-29
下一篇 2023-05-29

发表评论

登录后才能评论

评论列表(0条)

保存