How to convert numeric date into format 'dd-mmm-yyyy' (e.g. 01-mar-1970)
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
更多回答(2 个)
Walter Roberson
2021-1-29
datetime(mat(:, 1),'convertfrom', 'datenum', 'format', 'dd-MMM-yyyy')
0 个评论
Joanna Przeworska
2021-1-29
2 个评论
Walter Roberson
2021-1-29
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
2021-1-29
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!