Convert dates from Excel to Matlab

前端 未结 3 931
醉梦人生
醉梦人生 2021-01-15 05:29

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 a

3条回答
  •  礼貌的吻别
    2021-01-15 05:52

    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...

    enter image description here

    ... 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
    

提交回复
热议问题