Convert duration from MM:SS to M:SS.

1 回表示 (過去 30 日間)
Gianluca Finotti
Gianluca Finotti 2018 年 7 月 12 日
コメント済み: Gianluca Finotti 2018 年 7 月 13 日
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 Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by