Replace Before A Specific Character

6 visualizzazioni (ultimi 30 giorni)
CHRISTY il 28 Feb 2024
Commentato: Stephen23 il 28 Feb 2024
I have a string of calendar duraton values and am trying to replace the year value (Because I did some separate calculations on it). I want to replace the value given by the calendar duration function with my calculated value. The value I want to replace is the number up until "y" in this string: diffStr=4026y 4 mo 13d 9h 26m 20.145s"
Is there a replaceBefore function that I can use to replace everything before the "y"?
1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Stephen23 il 28 Feb 2024
Modificato: Stephen23 il 28 Feb 2024
"I have a string of calendar duraton values"
Calendar duration objects are not string objects. It is unclear what data you really have: descriptions of data are often very unreliable.... whereas having the actual data is always accurate.
Please upload some sample data in a MAT file, by clicking the paperclip button.

Accedi per commentare.

Risposte (4)

Cris LaPierre il 28 Feb 2024
Modificato: Cris LaPierre il 28 Feb 2024
Is it a duration or a string?
For a duration
Just to give a different solution than the others, here is one way to update a duration without changing the datatype. datevec works for matrix inputs, too.
d = calendarDuration(4026,5,13,9,26,20.145)
d = calendarDuration
4026y 5mo 13d 9h 26m 20.145s
[Y,M,D,H,Mn,S] = datevec(d);
newYr = 2021;
D = calendarDuration(newYr,M,D,H,Mn,S)
D = calendarDuration
2021y 5mo 13d 9h 26m 20.145s
For a string
Also works for matrix inputs
diffStr="4026y 4 mo 13d 9h 26m 20.145s"
diffStr = "4026y 4 mo 13d 9h 26m 20.145s"
newYr = "2021"; % must be a string
newstr = replaceBetween(diffStr,1,pat,newYr)
newstr = "2021y 4 mo 13d 9h 26m 20.145s"
1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Stephen23 il 28 Feb 2024
Without DATEVEC:
cD = calendarDuration(4026,5,13,9,26,20.145)
cD = calendarDuration
4026y 5mo 13d 9h 26m 20.145s
[~,m,d,t] = split(cD,{'years','months','days','time'})
m = 5
d = 13
t = duration
09:26:20
newYr = 2021;
D = calendarDuration(newYr,m,d)+t
D = calendarDuration
2021y 5mo 13d 9h 26m 20.145s

Accedi per commentare.

Star Strider il 28 Feb 2024
Assuming that you want to replace it with '9999'
diffStr="4026y 4 mo 13d 9h 26m 20.145s"
diffStr = "4026y 4 mo 13d 9h 26m 20.145s"
val = num2str(9999)
val = '9999'
diffStr = regexprep(diffStr, '\d*(?=y)', val)
diffStr = "9999y 4 mo 13d 9h 26m 20.145s"
You can of course just stubstitute in '9999' in the strrep call, however I assume that you want to do this to more than one number, (and probably in a loop, since I doubt regexprep is vectorised).
.
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Voss il 28 Feb 2024
% calendarDuration array
d = calendarDuration(21:24,2,28,10,6,27.34).'
d = 4×1 calendarDuration array
21y 2mo 28d 10h 6m 27.34s 22y 2mo 28d 10h 6m 27.34s 23y 2mo 28d 10h 6m 27.34s 24y 2mo 28d 10h 6m 27.34s
% string array
dStr = string(d)
dStr = 4×1 string array
"21y 2mo 28d 10h 6m 27.34s" "22y 2mo 28d 10h 6m 27.34s" "23y 2mo 28d 10h 6m 27.34s" "24y 2mo 28d 10h 6m 27.34s"
% replace the years in the strings with 13
new_year = 13;
dStr = regexprep(dStr,'\d+y',sprintf('%dy',new_year))
dStr = 4×1 string array
"13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s"
1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Steven Lord il 28 Feb 2024
Note that this change doesn't affect d.
% calendarDuration array
d = calendarDuration(21:24,2,28,10,6,27.34).'
d = 4×1 calendarDuration array
21y 2mo 28d 10h 6m 27.34s 22y 2mo 28d 10h 6m 27.34s 23y 2mo 28d 10h 6m 27.34s 24y 2mo 28d 10h 6m 27.34s
% string array
dStr = string(d);
% replace the years in the strings with 13
new_year = 13;
dStr = regexprep(dStr,'\d+y',sprintf('%dy',new_year))
dStr = 4×1 string array
"13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s"
d
d = 4×1 calendarDuration array
21y 2mo 28d 10h 6m 27.34s 22y 2mo 28d 10h 6m 27.34s 23y 2mo 28d 10h 6m 27.34s 24y 2mo 28d 10h 6m 27.34s
If you want to change d I'd probably split the calendarDuration object into its components using datevec, modify the numeric data, and recreate the calendarDuration object.
DV = datevec(d)
DV = 4×6
21.0000 2.0000 28.0000 10.0000 6.0000 27.3400 22.0000 2.0000 28.0000 10.0000 6.0000 27.3400 23.0000 2.0000 28.0000 10.0000 6.0000 27.3400 24.0000 2.0000 28.0000 10.0000 6.0000 27.3400
DV(:, 1) = 13;
d2 = calendarDuration(DV)
d2 = 4×1 calendarDuration array
13y 2mo 28d 10h 6m 27.34s 13y 2mo 28d 10h 6m 27.34s 13y 2mo 28d 10h 6m 27.34s 13y 2mo 28d 10h 6m 27.34s

Accedi per commentare.

If you want to change your string starting from the first character until 'y', you can use strfind with replaceBetween. If you want to "index into the string", you have to convert it to char:
diffStr="4026y 4 mo 13d 9h 26m 20.145s";
indexOfY=strfind(diffStr,'y');
diffChar=char(diffStr);
year=diffChar(1:indexOfY-1);
someValueExample=str2num(year)-100;
newDiffStr=replaceBetween(diffStr,1,indexOfY,strcat(num2str(someValueExample),"asd"))
newDiffStr = "3926asd 4 mo 13d 9h 26m 20.145s"
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Calendar in Help Center e File Exchange

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by