How to remove the year from a datetime table column

I have a datetime table with
'01-Oct-2014'
'02-Oct-2014'
'03-Oct-2014'
'04-Oct-2014'
'05-Oct-2014'
and I would like to get the same table (or an additional column in that table) but with day-month only for each row

7 comentarios

Simply change the format of the data -
%Data
y = datetime(["01-Oct-2014";
"02-Oct-2014"
"03-Oct-2014"
"04-Oct-2014"
"05-Oct-2014"])
y = 5×1 datetime array
01-Oct-2014 02-Oct-2014 03-Oct-2014 04-Oct-2014 05-Oct-2014
%Change the format of the datetime array
y.Format = 'dd-MMM';
y
y = 5×1 datetime array
01-Oct 02-Oct 03-Oct 04-Oct 05-Oct
Paul Barrette
Paul Barrette el 18 de Sept. de 2023
Movida: Dyuman Joshi el 20 de Sept. de 2023
Thanks @Dyuman Joshi. However, this code simply changes the formatting, but the underlying year data is still stored in the DATETIME object. What I'm trying to do is extract the day-month from each value.
Dyuman Joshi
Dyuman Joshi el 18 de Sept. de 2023
Movida: Dyuman Joshi el 20 de Sept. de 2023
Well, then you will have to get the day and the month data separately and store in a format you want to, as the datetime data type requires year as an input.
So, the question is how do you want store them?
Star Strider
Star Strider el 18 de Sept. de 2023
Movida: Dyuman Joshi el 20 de Sept. de 2023
The day and month functions exist.
Dyuman Joshi
Dyuman Joshi el 18 de Sept. de 2023
Movida: Dyuman Joshi el 20 de Sept. de 2023
Yes, I was going to provide that in my comment above, but thought that it would be better to answer after OP provides details on what the expected outcome is.
Paul Barrette
Paul Barrette el 18 de Sept. de 2023
Movida: Dyuman Joshi el 20 de Sept. de 2023
Sorry, I should have provided more info. I am plotting (with the 'scatter' command) five traces on the same graph, one per year (e.g., 2014 to 2018 inclusive). The horizontal axis for each year ranges from 01-Oct to 01-June (values for the vertical axis are from another matrix). But because the year is part of the object, these traces plot one after the other along that axis, not one on top of each other, which is what I'm looking for. To answer your question, I would store them as 'day-month' if only I could extract this from the current 'day-month-year' data.
ymd, as suggested earlier by @Star Strider, turns the datetime values into numeric arrays, but I'd like the 'day-month' label to appear on the horizontal axis of the graph.
How about this?
%Data
y = datetime(["01-Oct-2014";
"02-Oct-2014"
"03-Oct-2014"
"04-Oct-2014"
"05-Oct-2014"])
y = 5×1 datetime array
01-Oct-2014 02-Oct-2014 03-Oct-2014 04-Oct-2014 05-Oct-2014
%Get the day
d = day(y)
d = 5×1
1 2 3 4 5
%Get the month, with the name in short form
m = month(y,'shortname')
m = 5×1 cell array
{'Oct'} {'Oct'} {'Oct'} {'Oct'} {'Oct'}
%Single Quotation marks gives cell array
%Double Quotation marks gives string array
z = compose("%d-%s", d, string(m))
z = 5×1 string array
"1-Oct" "2-Oct" "3-Oct" "4-Oct" "5-Oct"

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 18 de Sept. de 2023
Editada: Star Strider el 18 de Sept. de 2023
The ymd function may be worth exploring.
EDIT — (18 Sep 2023 at 23:08)
Here is an example that also includes a scatter plot —
DT = datetime(['01-Oct-2014'
'02-Oct-2014'
'03-Oct-2014'
'04-Oct-2014'
'05-Oct-2018'
'01-Oct-2018'
'02-Oct-2018'
'03-Oct-2018'
'04-Oct-2018'
'05-Oct-2018']);
T1 = table(DT,randn(size(DT))+[zeros(5,1);4*ones(5,1)])
T1 = 10×2 table
DT Var2 ___________ _________ 01-Oct-2014 -0.082131 02-Oct-2014 0.47899 03-Oct-2014 0.25127 04-Oct-2014 0.99845 05-Oct-2018 0.39241 01-Oct-2018 4.2423 02-Oct-2018 5.3109 03-Oct-2018 4.1244 04-Oct-2018 3.8779 05-Oct-2018 4.609
[y,m,d] = ymd(DT); % Date Components
ix = findgroups(m, d); % Indices Of Months & Days
scattery = accumarray(ix, T1{:,2}, [], @(x){x}); % Accumulate 'Var2' Values By Day & Month
mc = month(DT,'shortname');
% compose('%s-%02d',string(mc),day(DT))
x = unique(compose('%s-%02d',string(mc),day(DT)),'stable');
dtx = datetime(x,'InputFormat','MMM-dd');
y = cell2mat(scattery.'); % Convert Cell Array To Numeric Array & Transpose
figure
scatter(dtx, y(1,:), 's', 'filled', 'DisplayName','2014')
hold on
scatter(dtx, y(2,:), 's', 'filled', 'DisplayName','2018')
hold off
grid
legend('Location','best')
I am not certain what you want to do.
.

2 comentarios

This works - thanks @Star Strider. Though this is intricate coding for me (an opportunity the learn!). I did try the solution offered by @Dyuman Joshi, much simpler, but I could not make it work.
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 17 de Sept. de 2023

Comentada:

el 20 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by