Info

This question is locked. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

handle character minus character

조회 수: 1 (최근 30일)
Ancalagon8
Ancalagon8 2020년 7월 10일
Locked: Rena Berman 2025년 1월 13일
I have two characters which contain '18:29:42.835' and '18:29:49.425'. How is it possible to subtrack t2-t1 so as to find duration?
  댓글 수: 1
Rena Berman
Rena Berman 2025년 1월 13일

(Answers Dev) Restored edit

채택된 답변

madhan ravi
madhan ravi 2020년 7월 10일
f = @(x) str2double(regexp(x,':','split'));
t1 = num2cell(f(t1)); % where t1 & 2 are your char arrays
t2 = num2cell(f(t2));
Wanted = duration(t2{:})-duration(t1{:})
  댓글 수: 1
madhan ravi
madhan ravi 2020년 7월 10일
f = @(x) str2double(regexp(x,'\D','split'));
t1 = num2cell(f(t1));
t2 = num2cell(f(t2));
Wanted = duration(t2{:})-duration(t1{:});
Wanted.Format = 'hh:mm:ss.SSSS'

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 7월 10일
If you have a recent enough MATLAB version, pass the character vectors to duration() and then subtract one from the other.
If your MATLAB is a bit older, then you will need to split the vectors at the colons, then str2double() to get the individual components, and pass those to duration.
Or you could use sscanf() with a format of '%f:%f:%f' to get the numeric components and pass those to duration()

This question is locked.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by