Convert duration from MM:SS to M:SS.

1 次查看(过去 30 天)
Hello everyone, I have a duration vector like this:
00:00
00:01
00:01
00:02
00:02
00:03
00:03
00:03
00:05
It only goes from 0 to 4 minutes so, in the plots I don't want to show something like: 01:05, but rather 1:05. That is, I want to get rid of the first digit and convert from mm:ss to m:ss. Is this possible?
Thank you for your time, Gianluca

回答(1 个)

Florian Floh
Florian Floh 2018-7-13
Hello!
In order to achieve the desired time-format (or date format) you have to take a closer look at the function "datetime".
I tried the following code by myself and it should give the result you wish to achieve:
%Code
% Set up the vector (just for me to test) containing the time 'mm:ss'
a= ["01:00";
"01:01";
"01:01";
"01:02";
"01:02";
"01:03";
"01:03";
"01:03";
"01:05"];
% get the size of a
[rowsA colsA] = size(a);
% loop through the array in order to change the time-format
for i=1: rowsA
a(i)= datetime(a(i), 'InputFormat', 'mm:ss', 'Format', 'm:ss');
end
I hope this answer was helpful.
Kind regards, Florian
  1 个评论
Gianluca Finotti
Gianluca Finotti 2018-7-13
I forgot to mention that the class of the variable is "duration", so that code does not really work in this case. But I think I have found the solution: |
a.Format = 'm'
I still don't know whether I like it because in this case I get:
0 min
0.016667 min
0.016667 min
0.033333 min
0.033333 min
0.05 min
I think I'd rather have just the number without the "min". I don't know whether that is going to create problems with the plots. But we'll see.
Anyways, thank you very much for your answer.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by