apply datestr to each element of a array in matlab

You can convert the array elements to datetime format and then use datestr function to convert each element to a string.

Here's an example:

main.m
% Example data
dates = [datenum('2021-08-01'); datenum('2022-09-15'); datenum('2023-12-31')];

% Convert to datetime format
dt = datetime(dates, 'ConvertFrom', 'datenum');

% Apply datestr to each element
date_strings = datestr(dt, 'yyyy-mm-dd');

% Display output
disp(date_strings)
284 chars
12 lines

Output:

main.m
2021-08-01
2022-09-15
2023-12-31
33 chars
4 lines

In the example above, datenum function is used to convert date strings to serial date numbers. Then, the serial date numbers are converted to datetime format using the datetime function. Finally, datestr function is used to convert each datetime element to a string with the specified format.

related categories

gistlibby LogSnag