I'm trying to convert date numbers to character dates, I created a vector using datenum and manually entering each date in Y,M,D,H,M,S format, and it works but I hope to find

5 visualizzazioni (ultimi 30 giorni)
Hi I'm still a newbie, I'm trying to convert date numbers to character dates, I created a vector using datenum and manually entering each date in Y,M,D,H,M,S format, and it works BUT, I have a ton of files and it takes me forever to write and add to the vector each day in Y,M,D,H,M,S format, because I have to manually change each day everytime I run a different set of files.
This is what I have and it works:
dvec = [ datenum(2018,07,28,02,27,32); datenum(2018,07,28,19,18,50); datenum(2018,07,28,21,27,19); datenum(2018,07,28,14,08,06); ...];
I've been going over the documenation I could find and this is what I'm trying but it doesn't work (it just shows the last date in the struct):
DateNumber = daily_names.datenum;
formatOut = 'mm dd';
str = datestr(DateNumber,formatOut);
Ps.
daily_names is a struct with datenum as a column and what I'm ultimately trying to do, is to automatically have the X axis in a plot, show the dates of the data.
Thank you!! All advice is appreciated

Risposte (2)

Steven Lord
Steven Lord il 3 Feb 2022
Instead of using serial date numbers with datenum, use datetime.
% Arbitrary data. I entered this manually, but you could build these
% vectors automatically by reading from a file (for example) or using the
% normal vector creation tools like the colon operator (see y)
y = (2020:2022).';
mo = [6; 3; 11];
d = [4; 12; 1];
h = [4; 8; 5];
mi = [15; 16; 23];
s = [42; 20; 01];
T = datetime(y, mo, d, h, mi, s)
T = 3×1 datetime array
04-Jun-2020 04:15:42 12-Mar-2021 08:16:20 01-Nov-2022 05:23:01
You can directly plot with datetime arrays.
plot(T, 1:3, 'o-')

Stephen23
Stephen23 il 3 Feb 2022
Modificato: Stephen23 il 3 Feb 2022
As Steven Lord and Walter Roberson and the MATLAB documentation recommended, you should use DATETIME objects, which can be plotted directly (no need to convert to text).
You can simply convert your structure data like so:
T = datetime([daily_names.datenum], 'ConvertFrom','datenum', 'Format','MM dd')
Avoid using the less accurate, less versatile, deprecated functions DATENUM, DATEVEC, and DATESTR. The recommended DATETIME objects support many many functions and can be plotted directly: read the help to know more about them:
  1 Commento
Margarita Rivera
Margarita Rivera il 3 Feb 2022
Thank you Steven!! For some reason it shows the date the data was downloaded as opposed to the date the data is from, but I agree it should work. The dates I need are in the datenum column in daily_names, and the date the data was downloaded is in a different column, so I'm not sure why its happening.
But I appreciate your help, I'll keep at it, thank you!

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by