if statement with sub cases
1 view (last 30 days)
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.
Accepted Answer
per isakson
on 16 Jun 2019
Edited: per isakson
on 20 Jun 2019
Try
%%
text = 'Time = 16:0:16 Height';
%%
num = sscanf( text, 'Time = %d:%d:%d Height' );
time = [ 60*60, 60, 1 ] * num;
added in response to comment
%%
text = 'Time = 16:0:16 Height';
%%
num = sscanf( text, 'Time = %d:%d:%d Height' );
t1 = [ 60*60, 60, 1 ] * reshape( num, [],1 );
%%
cac = regexp( text, '(?<=Time = )(\d+):(\d+):(\d+)(?= Height)', 'tokens' );
t2 = [ 60*60, 60, 1 ] * reshape( str2double(cac{1}), [],1 );
%%
t1==t2
outputs
ans =
logical
1
In response to request in separate answer
Run
>> tic, S = read_pco_header( 'h:\m\cssm\pco.txt' ); toc
Elapsed time is 0.011812 seconds.
and peek on the result
>> S
S =
struct with fields:
FileName: "D:\experiment\MoleculeExp\FEM\Video\v190322ad\v190322ad_0.pco"
File_version: "1"
Date: "22.3.2019"
Time: 54745
Height: 600
Width: 800
Binning: 1
Exposure_Time: 1000
Average: 5
Temperature: 45
Pixel_Rate: 12
Tip_Voltage: 2100
Shutter_State: 0
>>
and
>> S.Time
ans =
54745
>>
where
function S = read_pco_header( filespec )
%{
S = read_pco_header( 'h:\m\cssm\pco.txt' );
%}
S = struct( ...
'FileName' , {[]} ...
, 'File_version' , {[]} ...
, 'Date' , {[]} ...
, 'Time' , {[]} ...
, 'Height' , {[]} ...
, 'Width' , {[]} ...
, 'Binning' , {[]} ...
, 'Exposure_Time' , {[]} ...
, 'Average' , {[]} ...
, 'Temperature' , {[]} ...
, 'Pixel_Rate' , {[]} ...
, 'Tip_Voltage' , {[]} ...
, 'Shutter_State' , {[]} ...
);
fid = fopen( filespec, 'rt' );
chr = fscanf( fid, '%c' , 2048 );
fclose( fid );
string_array = string( strsplit( chr, '\n' ) );
for str = string_array
tokens = split( str, ["=",":"] );
key = strtok( tokens(1) );
switch key
case "PCO"
% do nothing
case "FileName"
% The separator, ":", between name and value ...
S.FileName = strtrim(tokens(2)+":"+tokens(3));
case "File_version"
S.File_version = strtrim(tokens(2));
case "Date"
S.Date = strtrim(tokens(2));
case "Time"
S.Time = [3600,60,1]*reshape( str2double(tokens([2,3,4])), [],1 );
case "Height"
S.Height = str2double(tokens(2));
case "Width"
S.Width = str2double(tokens(2));
case "Binning"
S.Binning = str2double(tokens(2));
case {"Exposure","Exposure_Time"}
S.Exposure_Time = str2double(strtok(tokens(2)));
case "Average"
S.Average = str2double(tokens(2));
case "Temperature"
S.Temperature = str2double(tokens(2));
case {"Pixel","Pixel_Rate"}
S.Pixel_Rate = str2double(tokens(2));
case {"Tip","Tip_Voltage"}
S.Tip_Voltage = str2double(tokens(2));
case {"Shutter","Shutter_State"}
S.Shutter_State = str2double(tokens(2));
case "End"
% do nothing
case ""
% do nothing
otherwise
error( 'read_pco_header:UnknownKey' ...
, 'Unexpected key, "%s", in "%s%"' ...
, key, filespec )
end
end
end
Afterthought
I uploaded a somewhat improved version of read_pco_header
and here is a version of the "regexp-script" that works with the test file
%%
ffs = 'h:\m\cssm\pco.txt';
fid = fopen( ffs, 'rt' );
chr = fscanf( fid, '%c' , 2048 );
fclose( fid );
cac = regexp( chr, '(?<=Time = )(\d+):(\d+):(\d+)\s+Height', 'tokens' );
t2 = [ 60*60, 60, 1 ] * reshape( str2double(cac{1}), [],1 );
%%
It feels like overkill to include "Height" in the regular expression. However, it's included in the original question.
14 Comments
per isakson
on 17 Jun 2019
I'm glad if it's useful and will be used.
I uploaded a somewhat improved version of read_pco_header
More Answers (1)
See Also
Categories
Find more on Characters and Strings 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!