How to use 'setvartype' to get the variable as 'datetime' formatted as: yyyy-MM-dd

20 visualizaciones (últimos 30 días)
I've got a spreadsheet which gets imported into Matlab via readtable().
Prior to importing I use detectImportOptions() and setvartype() to change the type of certain variables.
Everything works well but one thing, I want the date column of my data to be imported as 'yyyy-MM-dd', but by default I get it gives me 'dd-MM-yyy-hh-mm-ss'.
Problem: I want to ditch the hour-minute-second bit of the date as I don't need it. How to do it via detectImportOptions?
My code:
FileName = 'AllTestData.xlsx';
% detect 'READTABLE' import options for the given sheet
opts = detectImportOptions(FileName);
opts = setvartype(opts,{'Test_Date',},'datetime');
% Import data:
tAllTestData = readtable(FileName,opts);
My current workaround is that, I add this line at the end of code:
tAllTestData.Test_Date = datetime(tAllTestData1.Test_Date,'format', 'yyyy-MM-dd');
But like I said, I'd like to get all the import settings adjusted prior to importing.
To illustrate my problem:

Respuesta aceptada

Harsh
Harsh el 25 de Jul. de 2018
One way is to change the datetime defaults before importing. More on this is here .
For example:
>> datetime.setDefaultFormats('default','yyyy-MM-dd');
>> FileName = 'AllTestData.xlsx';
>> opts = detectImportOptions(FileName);
>> opts = setvartype(opts,{'Test_Date'},'datetime');
>> tAllTestData = readtable(FileName,opts);
>> head(tAllTestData)
ans =
8×2 table
Nr Test_Date
__ __________
1 2017-08-24
2 2017-08-29
3 2017-08-31
4 2017-09-01
5 2017-09-04
6 2017-09-05
7 2017-12-07
8 2017-12-12
  3 comentarios
Harsh
Harsh el 25 de Jul. de 2018
Just to add, alternatively, you could also do the following:
..
>> opts = setvartype(opts,{'Test_Date'},'datetime');
>> opts = setvaropts(opts,{'Test_Date'},'InputFormat','yyyy-MM-dd');
..
Pawel Jastrzebski
Pawel Jastrzebski el 26 de Jul. de 2018
Once again thank you. I like the alternative approach better so I'll stick with it. And it's a bit faster base on some tests I've run.

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 3 de Ag. de 2018
Pawel, if I understand your question correctly, this is just a display issue. The datetime in the middle and right images are the same value, it's just the the right-hand one doesn't display HH:mm:ss. One simple thing to do is set the format after importing:
tallTestData.TestDate.Format = 'dd-MMM-yyyy'
Maybe that's what you've already done, and you wanted to do it as part of the import. But just to be clear: all that is is a display format.
  1 comentario
Pawel Jastrzebski
Pawel Jastrzebski el 6 de Ag. de 2018
Peter,
thanks for the suggestion. I've eventually followed Harsh's alternative solution as it made most sense to me to have the date format set prior to importing the data so when it's already imported in Matlab, I don't need to do any additional manipulations.

Iniciar sesión para comentar.

Productos


Versión

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by