## if statement with sub cases

Mohammed Qahosh

### Mohammed Qahosh (view profile)

님이 질문을 제출함. 16 Jun 2019
최근 활동 per isakson

### per isakson (view profile)

님이 편집함. 20 Jun 2019
per isakson

### per isakson (view profile)

님이 답변을 채택함.
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

per isakson

### per isakson (view profile)

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

### Mohammed Qahosh (view profile)

16 Jun 2019
Hi Per isakson, this is one of the cases and as time goes on the numbers change. and I am using matlab R2013 release
text='Time = 16:0:16 Height'

로그인 to comment.

## 답변 수: 2

per isakson

### per isakson (view profile)

님의 답변 16 Jun 2019
per isakson

### per isakson (view profile)

님이 편집함. 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:
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
%{
%}
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
, 'Unexpected key, "%s", in "%s%"' ...
, key, filespec )
end
end
end
Afterthought
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.

표시 이전 댓글 수: 11
per isakson

### per isakson (view profile)

16 Jun 2019
Try to describe the problem in plain English.
• Descibe what is given; what's the input. Distinguish between static text, e.g. "Time =", and dynamic text, e.g. "16:0:16".
• Describe what you want; what shall be the output.
I'm not going to backward engineer code that isn't even working. What has for example 'SBIG' to do with "I need the time for each picture" ?
Upload (with the paper-clip-button) a couple of examples of 2018 character texts
Mohammed Qahosh

### Mohammed Qahosh (view profile)

16 Jun 2019
per isakson Really thank you. This is too much. I appreciate your help :)))
per isakson

### per isakson (view profile)

17 Jun 2019
I'm glad if it's useful and will be used.

로그인 to comment.

Mohammed Qahosh

### Mohammed Qahosh (view profile)

님의 답변 16 Jun 2019

*I have thousands of pictures in the pco format
*I am using these pictures to find the change in the intensity
*each of these pictures has informations : Time, Voltage , .......
*I need to find the Time of at which each picture was taken
*from the code already exists the previous student found the width, height and exposure time
*Now I need to find the time
PCO Pixelfly USB Image
File_version = 1
Date = 22.3.2019
Time = 15:12:25
Height = 600
Width = 800
Binning = 1
Exposure Time = 1000 msec
Average = 5
Temperature = 45
Pixel Rate = 12
Tip Voltage = 2100
Shutter State = 0
End
These are the data contained in each picture. But the time changes
So, how can I use his code to get the time?

로그인 to comment.

Translated by