问题描述
我有一系列的日期和一些相应的值。 Excel中的数据格式为Custom dd / mm / yyyy hh:mm
。 当我尝试在Matlab中将此列转换为数组时,为了将其用作图的x轴,我使用:
a = datestr(xlsread('filename.xlsx',1,'A:A'),'dd / mm / yyyy HH:MM');
但是我得到一个空字符串:0-by-16。
因此,我无法使用函数 datenum
将其转换为日期数组。
我在哪里犯错?编辑:从hh:mm传递到HH:MM也不行。当我只尝试
a = xlsread('filename.xlsx',1,'A2')
我得到:a = []
根据分钟,月和小时的语法如下:
HH - >小时两位数
MM - >分两位数
mm - >两位数
因此,您必须更改调用中的 datestr
。由于Excel和Matlab之间的序列号格式不一致,因此您必须向 xlsread $ c $中的检索到的数字添加一个
693960
的偏移量c>。
dateval = xlsread('test.xls',1,'A:A')+ 693960;
datestring = datestr(dateval,'dd / mm / yyyy HH:MM');
这将读取第一列( A
)的Excel文件中的第一页( 1
)。为了获得更好的效果,您可以明确指定范围(例如'A1:A20'
)。
代码转换...
...至:
datestring =
22/06/2015 16:00
编辑:以下代码适用于您提供的Excel文件:
%从文件$ b读取$ b tbl = readtable('data.xls','ReadVariableNames',false);
dateval = tbl。(1);
dateval = dateval + 693960;
datestring = datestr(dateval)
%dateticks为x轴
plot(dateval,tbl。(2))
datetick('x' 'mmm / yy')
%datetick('x','dd / mmm / yy')%这可能比只有月份
I have a series of dates and some corresponding values. The format of the data in Excel is "Custom" dd/mm/yyyy hh:mm
.When I try to convert this column into an array in Matlab, in order to use it as the x axis of a plot, I use:
a = datestr(xlsread('filename.xlsx',1,'A:A'), 'dd/mm/yyyy HH:MM');
But I get a Empty string: 0-by-16.
Therefore I am not able to convert it into a date array using the function datenum
.
Where do I make a mistake? Edit: passing from hh:mm to HH:MM doesn't work neither. when I try only
a = xlsread('filename.xlsx',1,'A2')
I get: a = []
According to the documentation of datestr
the syntax for minutes, months and hours is as follows:
HH -> Hour in two digits
MM -> Minute in two digits
mm -> Month in two digits
Therefore you have to change the syntax in the call for datestr
. Because the serial date number format between Excel and Matlab differ, you have to add an offset of 693960
to the retrieved numbers from xlsread
.
dateval = xlsread('test.xls',1,'A:A') + 693960;
datestring = datestr(dateval, 'dd/mm/yyyy HH:MM');
This will read the first column (A
) of the first sheet (1
) in the Excel-file. For better performance you can specify the range explicitly (for example 'A1:A20'
).
The code converts...
... to:
datestring =
22/06/2015 16:00
Edit: The following code should work for your provided Excel-file:
% read from file
tbl = readtable('data.xls','ReadVariableNames',false);
dateval = tbl.(1);
dateval = dateval + 693960;
datestring = datestr(dateval)
% plot with dateticks as x-axis
plot(dateval,tbl.(2))
datetick('x','mmm/yy')
%datetick('x','dd/mmm/yy') % this is maybe better than only the months
这篇关于将日期从Excel转换为Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!