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 Comments

per isakson
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.
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'

Sign in to comment.

 Accepted Answer

per isakson
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

I have the following error:
Error using *
Inner matrix dimensions must agree.
Error in OpenImage (line 11)
time = [ 60*60, 60, 1 ] * num;
Did you use a different value of text ?
What does
whos num
show?
but what does that mean to have 1
You rely on me guessing.
What produced the output you show? Not whos num anyhow.
If num is a <3x1> column vector, as in your comment, the error
Error using *
Inner matrix dimensions must agree.
Error in OpenImage (line 11)
time = [ 60*60, 60, 1 ] * num;
does not happen. I'm positive that when the error was thrown num had another value.
Are you able to reproduce this
>> [ 60*60, 60, 1 ] * [16;0;16]
ans =
57616
per isakson
per isakson on 16 Jun 2019
Edited: per isakson on 16 Jun 2019
"but what does that mean to have 1" a semicolon at the end of a line is missing
Maybe it was t1==t2 in which case it indicated that t1 and t2 are equal.
per isakson Thank you very much for your help. The best help I've gotten.
per isakson Sorry again, but I am new to matlab and I need this.
The test I showed to you is just an example. The code from the previous student starts with this:
function [Parameter,Image,time]=OpenImage(name)
a=fopen(name);
text=fscanf(a,'%c',2048);
and if I use the same code from you without that text I go back to the original problem
The code shows a function called, OpenImage, which
  • takes the name of a file as input
  • opens that file and
  • reads 2048 characters from the file to the variable text
It doesn't say anything about the content of this 2048 characters text. Most likely
num = sscanf( text, 'Time = %d:%d:%d Height' );
cannot handle this 2048 characters text. What do you know about this text?
Is "Time = 16:0:16 Height" embedded in the text?
In fact the time is not the same. I have thousands of pictures and each one has it's own time.
Here is the code. Sorry for asking many questions but I am lost ans I need this.
All pictures are pco type. I am calling thousands of them and I need the time for each picture.
function [Parameter,Image,time]=OpenImage(name)
a=fopen(name);
text=fscanf(a,'%c',2048);
if (strcmp(name(end-2:end),'pco')==1)
height=str2num(text(strfind(text,'Height =')+8:strfind(text,'Width')-1));
width=str2num(text(strfind(text,'Width =')+7:strfind(text,'Binning')-1));
exposure=str2num(text(strfind(text,'Exposure Time =')+16:strfind(text,'msec')-1));
end
if (strcmp(name(end-3:end),'SBIG')==1)
height=str2num(text(strfind(text,'Height =')+8:strfind(text,'Width')-1));
width=str2num(text(strfind(text,'Width =')+7:strfind(text,'Date')-1));
exposure=str2num(text(strfind(text,'Each_exposure =')+16:strfind(text,'x 10millsec')-1))*10;
end
fseek(a,2048,'bof');
Image=fread(a,[width height],'uint16')';
Parameter=[height,width,exposure,time];
fclose(a);
per isakson
per isakson on 16 Jun 2019
Edited: per isakson on 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
per isakson Really thank you. This is too much. I appreciate your help :)))
I'm glad if it's useful and will be used.
I uploaded a somewhat improved version of read_pco_header

Sign in to comment.

More Answers (1)

*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
FileName: D:\experiment\MoleculeExp\FEM\Video\v190322ad\v190322ad_0.pco
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?

Categories

Community Treasure Hunt

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

Start Hunting!