Borrar filtros
Borrar filtros

day and month inverted in the time scaling

4 visualizaciones (últimos 30 días)
Sébastien Ruiz
Sébastien Ruiz el 26 de Sept. de 2023
Comentada: Sébastien Ruiz el 29 de Sept. de 2023
time x array : 04/05/2023 00:00:22.384 04/05/2023 00:00:59.361 04/05/2023 00:02:33.408
When using stairs function the legend in x is displayed Apr 05, 2023 where it should be May 04, 2023.
How can I set the time display?

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 26 de Sept. de 2023
Extract the day and month from the datetime values and swap them.
%Example
in = [datetime(2023,4,5,00,00,22.384) datetime(2023,4,5,00,00,59.361) datetime(2023,4,5,00,02,33.408)]
in = 1×3 datetime array
05-Apr-2023 00:00:22 05-Apr-2023 00:00:59 05-Apr-2023 00:02:33
%Get year month and day
[y,m,d] = ymd(in)
y = 1×3
2023 2023 2023
m = 1×3
4 4 4
d = 1×3
5 5 5
%Get hour minute and second
[H,M,S] = hms(in);
out = datetime(y,d,m,H,M,S)
out = 1×3 datetime array
04-May-2023 00:00:22 04-May-2023 00:00:59 04-May-2023 00:02:33
  3 comentarios
Dyuman Joshi
Dyuman Joshi el 26 de Sept. de 2023
@Sébastien Ruiz, please consider accepting my answer then :)
Sébastien Ruiz
Sébastien Ruiz el 29 de Sept. de 2023
This solution worked and thus I applied it. Thanks for this tip.

Iniciar sesión para comentar.

Más respuestas (1)

Les Beckham
Les Beckham el 26 de Sept. de 2023
Editada: Les Beckham el 26 de Sept. de 2023
If you created your "time x array" by reading strings from a text file, for example, you need to specify the 'InputFormat' argument when you read the strings using the datetime function (see documentation here).
date_strings = ["04/05/2023 00:00:22.384", "04/05/2023 00:00:59.361", "04/05/2023 00:02:33.408"];
time_x_array = datetime(date_strings, 'InputFormat', 'dd/MM/yyyy hh:mm:ss.SSS')
time_x_array = 1×3 datetime array
04-May-2023 00:00:22 04-May-2023 00:00:59 04-May-2023 00:02:33
  2 comentarios
Steven Lord
Steven Lord el 26 de Sept. de 2023
Note that if you don't include the InputFormat, MATLAB will warn you that your text data is ambiguous. It can't tell whether the month or the date is first. It will even suggest the alternate format that also matches the text representation.
date_strings = ["04/05/2023 00:00:22.384", "04/05/2023 00:00:59.361", "04/05/2023 00:02:33.408"];
time_x_array = datetime(date_strings)%, 'InputFormat', 'dd/MM/yyyy hh:mm:ss.SSS')
Warning: Successfully converted the text to datetime using the format 'MM/dd/uuuu HH:mm:ss.SSS', but the format is ambiguous and could also be 'dd/MM/uuuu HH:mm:ss.SSS'. To create datetimes from text with a specific format call:

datetime(textinput,'InputFormat',infmt)
time_x_array = 1×3 datetime array
05-Apr-2023 00:00:22 05-Apr-2023 00:00:59 05-Apr-2023 00:02:33
While MATLAB Answers runs the most recent release of MATLAB, I confirmed that this warning appears in release R2021b as well.
Dyuman Joshi
Dyuman Joshi el 26 de Sept. de 2023
It appears in R2021a as well.

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by