if statement with sub cases
Show older comments
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 Comments
per isakson
on 16 Jun 2019
Edited: per isakson
on 16 Jun 2019
"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
on 16 Jun 2019
Accepted Answer
More Answers (1)
Mohammed Qahosh
on 16 Jun 2019
Categories
Find more on Marine and Underwater Vehicles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!