MATLAB: Changing ‘daytime’ data type to string string to export in excel

data exportdata typedatedaytimeexcelMATLAB

I am trying to export sensor data with its associated date and time to excel sheet but due to some reason the date is not getting displayed in excel sheet or in workspace. code:
filename='xbee1.xlsx';
d{1,1}='Date';
d{1,2}='Sensor Data';
c=clock;
c=round(c);
t = datetime(c(1),c(2),c(3));
data_sensor{1,1}=t;
data_sensor{1,2}=rand;
d=[d; data_sensor];
disp(d);
%xlRange = sprintf('A%i:G%i',j,k);
xlRange = 'A1';
xlswrite(filename,d,1,xlRange);
Thanks in advance..

Best Answer

  • If the datetimes are in a table, writetable will just work:
    >> Date = dateshift(datetime,'start','second') + seconds(0:4)';
    >> SensorData = rand(5,1);
    >> t = table(Date,SensorData)
    t =
    Date SensorData
    ____________________ __________________
    24-Apr-2015 19:13:13 0.0357116785741896
    24-Apr-2015 19:13:14 0.849129305868777
    24-Apr-2015 19:13:15 0.933993247757551
    24-Apr-2015 19:13:16 0.678735154857773
    24-Apr-2015 19:13:17 0.757740130578333
    >> writetable(t,'xbee2.xlsx')
    You'll have to go into the spreadsheet and set the formatting, but you do gets the right dates/times. (Note that datetime('now'), or just datetime, is probably simpler than calling clock and converting, although you'd have to use dateshift to round to whole seconds.)
    Or, use char or cellstr on your datetimes.
    Hope this helps.