How to convert numeric date into format 'dd-mmm-yyyy' (e.g. 01-mar-1970)

1 view (last 30 days)
Hi,
Suppose I have a double 10x2 that contains numeric dates in the first column and numbers in the second, like below.
My goal is to change numeric dates into format 'dd-mmm-yyyy' (e.g. 01-mar 1970), but I want to do that within double 'mat'. I don't want to create any new variable.
Just simply change the format of mat(:,1). Is it possible to do that with only one code line? I tried many options, however, each ended with an error.
I would be grateful for your advices.
mat =
1.0e+05 *
7.2866 0.4346
7.2875 0.4399
7.2884 0.4434
7.2893 0.4480
7.2903 0.4497
7.2912 0.4550
7.2921 0.4582
7.2930 0.4613
7.2939 0.4648
7.2948 0.4680

Accepted Answer

Joanna Przeworska
Joanna Przeworska on 1 Feb 2021
Dear Walter and Steven,
Thank you for your suggestions. I implement one of them for sure.
Best regards, JP

More Answers (2)

Walter Roberson
Walter Roberson on 29 Jan 2021
datetime(mat(:, 1),'convertfrom', 'datenum', 'format', 'dd-MMM-yyyy')

Joanna Przeworska
Joanna Przeworska on 29 Jan 2021
Dear Walter,
Thanks a lot for your suggestion. This solution works perfectly fine when I create a new variable.
However, my goal is to change format directly in double 'mat', not creating any new variable. I tried this (below), but got the following error:
mat(:,1) = datetime(mat(:,1),'convertfrom', 'datenum', 'format', 'dd-MMM-yyyy')
The following error occurred converting from datetime to double:
Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a
datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions.
  2 Comments
Walter Roberson
Walter Roberson on 29 Jan 2021
You cannot meet that goal. Your matrix is currently numeric, and you want the matrix to contain - and space such as 01-mar 1970 . However, - and space are not numeric, and cannot be stored in a numeric array (at least not and have it display as - and space.)
If you want your dates to display looking like dates, you have a few possibilities:
  • convert everything to character, second column included
  • create a table() object where the first variable is the datetime objects and the second is numeric
  • create a cell array in which the first element is a datetime array and the second is a numeric array. If you do this, there will be {} and [] displayed.
  • create a 2D cell array in which {K,1} is a single datetime object and {K,2} is a single numeric scalar. This will be displayed with lots of {} and []
  • Use fprintf() to display something (without storing it)
Steven Lord
Steven Lord on 29 Jan 2021
Creating a timetable using the first column as the RowTimes and the remaining columns as the data is another option, and that would enable the user to call functions like retime and synchronize on it as well as index into it using a timerange to extract rows whose times are between two specified times.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!