How to remove the year from a datetime table column
32 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Paul Barrette
am 17 Sep. 2023
Kommentiert: Star Strider
am 20 Sep. 2023
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 Kommentare
Dyuman Joshi
am 18 Sep. 2023
Verschoben: Dyuman Joshi
am 20 Sep. 2023
How about this?
%Data
y = datetime(["01-Oct-2014";
"02-Oct-2014"
"03-Oct-2014"
"04-Oct-2014"
"05-Oct-2014"])
%Get the day
d = day(y)
%Get the month, with the name in short form
m = month(y,'shortname')
%Single Quotation marks gives cell array
%Double Quotation marks gives string array
z = compose("%d-%s", d, string(m))
Akzeptierte Antwort
Star Strider
am 18 Sep. 2023
Bearbeitet: Star Strider
am 18 Sep. 2023
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)])
[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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!