# change the first digits when ends with 5959

2 ビュー (過去 30 日間)
vicente Noguer 2021 年 9 月 20 日
コメント済み: Stephen 2021 年 9 月 21 日
Hey, I've got a doubt. I have a string of numbers that go from 0000 to 5959(First two are the minuts and the other two the seconds), when it reaches another time 0000 I wanted to add a unit that goes from 1 to 23(that indicates the hours). I have added this line of code:
T_Hour_1 = '00'+t1; % it gives us t1 with two 00 that indicates the hour
then I got this lines of code that changes the string of T_Hour_1 to dur.
DN1 = str2double(t1);
dur1 = minutes(floor(DN1/100)) + seconds(mod(DN1,100)); % separar en minutos y segundos
[~, M1, S1] = hms(dur1); % Dar tiempo en 2 variables, utilizar M1 para crear rangos
Before doing DN1 I would like to add the numbers from 0 to 23 in front of the others.
How could I do it?

サインインしてコメントする。

### 採用された回答

Stephen 2021 年 9 月 21 日

Rather than messing around with strings you should probably just work with duration objects or numeric arrays.
S = ["000000";"000059";"000100";"000159";"005959";"000000";"005959";"000000";"005900"]
S = 9×1 string array
"000000" "000059" "000100" "000159" "005959" "000000" "005959" "000000" "005900"
D = duration(strcat(extractBefore(S,3),':',extractBetween(S,3,4),':',extractAfter(S,4)))
D = 9×1 duration array
00:00:00 00:00:59 00:01:00 00:01:59 00:59:59 00:00:00 00:59:59 00:00:00 00:59:00
D = D + hours(cumsum([0;diff(D)<0]))
D = 9×1 duration array
00:00:00 00:00:59 00:01:00 00:01:59 00:59:59 01:00:00 01:59:59 02:00:00 02:59:00
##### 4 件のコメント表示非表示 3 件の古いコメント
Stephen 2021 年 9 月 21 日
"could you explain to me the second line of code you gave me"
That line locates where the difference in duration is negative (i.e. the duration jumps from a larger value to a smaller value). It then uses CUMSUM on those jump points to create a vector of hourly increments.
If you do not understand how code works then take it apart and look at all of the intermediate results:
S = ["0000";"0059";"0100";"0159";"5959";"0000";"5959";"0000";"5900"];
D = duration(strcat('00:',extractBefore(S,3),':',extractAfter(S,2)))
D = 9×1 duration array
00:00:00 00:00:59 00:01:00 00:01:59 00:59:59 00:00:00 00:59:59 00:00:00 00:59:00
V = diff(D) % difference between adjacent durations.
V = 8×1 duration array
00:00:59 00:00:01 00:00:59 00:58:00 -00:59:59 00:59:59 -00:59:59 00:59:00
W = V<0 % locations where longer duration jumps back to shorter duration.
W = 8×1 logical array
0 0 0 0 1 0 1 0
X = [0;W] % pad with zero.
X = 9×1
0 0 0 0 0 1 0 1 0
Y = cumsum(X) % create numeric list of hourly jumps
Y = 9×1
0 0 0 0 0 1 1 2 2
Z = hours(Y) % convert to duration.
Z = 9×1 duration array
0 hr 0 hr 0 hr 0 hr 0 hr 1 hr 1 hr 2 hr 2 hr
D = D + Z % add to the original data
D = 9×1 duration array
00:00:00 00:00:59 00:01:00 00:01:59 00:59:59 01:00:00 01:59:59 02:00:00 02:59:00

サインインしてコメントする。

### その他の回答 (1 件)

Steven Lord 2021 年 9 月 20 日
Is there a reason you're starting with string data first rather than using a duration array from the start?
d = minutes(0:0.25:60);
d.Format = 'hh:mm:ss';
d(1:5)
ans = 1×5 duration array
00:00:00 00:00:15 00:00:30 00:00:45 00:01:00
d(end-4:end)
ans = 1×5 duration array
00:59:00 00:59:15 00:59:30 00:59:45 01:00:00
##### 2 件のコメント表示非表示 1 件の古いコメント
Stephen 2021 年 9 月 21 日
STR2DOUBLE is not really suitable for handling date/time values written as one string of digits.
You should be using duration objects to store time durations.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by