Convert dates from Excel to Matlab

前端 未结 3 929
醉梦人生
醉梦人生 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
    
    0 讨论(0)
  • 2021-01-15 05:55

    I tried with this. It works but it is slow and I am not able to plot the dates at the end. Anyway:

    table= readtable ('filename.xlsx');
    dates = table(:,1);
    dates = table2array (dates);
    dates = datenum(dates);
    dates = datestr (dates);
    
    0 讨论(0)
  • 2021-01-15 06:04

    Minutes need to be called with a capital M to distinguish them from months. Use a=datestr(xlsread('filename.xlsx',1,'A:A'),'dd/mm/yyyy HH:MM')

    Edit: Corrected my original answer, where I had mixed up the cases needed.

    0 讨论(0)
提交回复
热议问题