if statement with sub cases
이전 댓글 표시
I need to use the if statement in my code and I appreciate your help. I need it in the code like this:
I need to check the length of some thing, I have 3 cases but to of them have also 2 subcases as follows:
I have tow cases for L=17 and 2 cases for L==16 How can I manage that in the code ?
Thank you in advance for your help.
%%
A=regexp(text,'Time');
B=regexp(text,'Height');
L= B-A(1);
if L==17
C=strfind(text,'Time');
time_hour=str2num(text(C(1)+7:strfind(text,'Height')-9));
time_min=str2num(text(C(1)+10:strfind(text,'Height')-6));
time_sec=str2num(text(C(1)+13:strfind(text,'Height')-3));
time=60*60*time_hour + 60*time_min + time_sec;
elseif L==17
C=strfind(text,'Time');
time_hour=str2num(text(C(1)+7:strfind(text,'Height')-8));
time_min=str2num(text(C(1)+10:strfind(text,'Height')-6));
time_sec=str2num(text(C(1)+12:strfind(text,'Height')-3));
time=60*60*time_hour + 60*time_min + time_sec;
elseif L==15
C=strfind(text,'Time');
time_hour=str2num(text(C(1)+7:strfind(text,'Height')-7));
time_min=str2num(text(C(1)+10:strfind(text,'Height')-5));
time_sec=str2num(text(C(1)+12:strfind(text,'Height')-3));
time=60*60*time_hour + 60*time_min + time_sec;
elseif L==16;
C=strfind(text,'Time');
time_hour=str2num(text(C(1)+7:strfind(text,'Height')-8));
time_min=str2num(text(C(1)+10:strfind(text,'Height')-5));
time_sec=str2num(text(C(1)+13:strfind(text,'Height')-3));
time=60*60*time_hour + 60*time_min + time_sec;
elseif L==16
C=strfind(text,'Time');
time_hour=str2num(text(C(1)+7:strfind(text,'Height')-8));
time_min=str2num(text(C(1)+10:strfind(text,'Height')-6));
time_sec=str2num(text(C(1)+12:strfind(text,'Height')-3));
time=60*60*time_hour + 60*time_min + time_sec;
end
댓글 수: 2
per isakson
2019년 6월 16일
편집: per isakson
2019년 6월 16일
"I have two cases for L=17 and 2 cases for L==16" How do the two cases differ? I don't want to figure that out by backward engineering your script.
I believe that the task can be solved by a few lines of code, either with sscanf() or better use of regexp(). If such a solution would be helpful, please provide a handful of examples of the text that shall be parsed.
Mohammed Qahosh
2019년 6월 16일
채택된 답변
추가 답변 (1개)
Mohammed Qahosh
2019년 6월 16일
카테고리
도움말 센터 및 File Exchange에서 Marine and Underwater Vehicles에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!