Reading a text file containing colon mark

I have a text file (a) contains words and numbers and colons. I would like to use the fscan and the fread function to get a time. However, the time is like this 2:30:20
I wonder, how can I do that such that there are cono(:) marks in between.
B=fread(a,time,'uint16');
I think the problem is in the uint16, but I don't know how to solve that.
Thanks in advance for your help

답변 (1개)

Krishna Anne
Krishna Anne 2019년 5월 30일

0 개 추천

Looks like you are getting the time as "Duration" class, Try one of these as required to fetch hours or seconds or minutes or milliseconds etc.
milliseconds(time) or
seconds(time) or
hours(time) or
minutes(time)
..........etc, provided time has class 'duration' you may check by typing class(time)

댓글 수: 16

Thanks for replying. But actually it didn't work.
I am trying also to use the {extractBetween} like in the documantation :
but I got the following wrong message
The input character is not valid in MATLAB statements or expressions. which is this guy {"}
Which MATLAB release are you using? extractbetween() was added in R2016b, and ability to use " to delimit strings was added in R2017a.
I am using R2013 release :(
I suggest you look at fileread() and regexp()
In fact, I have dozens of pictures and each has its own time. ans the regexp can be used to get a well known character pattern which I don't know exactly. I think the best way is to get a new release so I can use easier functions specially the extractBetween function.
extractBetween is less flexibile than regexp is, so if there is a reason why you cannot use regexp then extractBetween will be even more difficult for you to get the information you need.
The time I would like to find is sandwiched between the two words : Time , Height
So, I need to use the following :
A= (strfind(text,'Time') : strfind(text,'Height'))
So I don't have a direct expression to mention as an argument for the function regexp to search inside the string (text)
Any suggestions? Totally thankful
Guillaume
Guillaume 2019년 6월 2일
Your implementation of the text parsing is very fragile. I agree with Walter, regexp is the way to go. If you attach an example file to your question, we can work out a reliable way to parse your text header.
Mohammed Qahosh
Mohammed Qahosh 2019년 6월 2일
편집: Rik 2019년 6월 2일
In fact I am using a code from a previous master student. This is the code: I have thousands of pictures and I need to find the time of each of them. The time is between the words (Time,Height)
Here is the code, and below is the data in the one of the original pictures.
function [Parameters,Image]=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')';
Parameters=[height,width,exposure];
fclose(a);
Data :
PCO Pixelfly USB ImageFileName: D:\experiment\MoleculeExp\FEM\Video\v190322ae\v190322ae_0.pcoFile_version = 1Date = 22.3.2019Time = 15:26:42Height = 600Width = 800Binning = 1Exposure Time = 1000 msecAverage = 5Temperature = 45Pixel Rate = 12Tip Voltage = 2150Shutter State = 0End
Rik
Rik 2019년 6월 2일
This time I edited your comment for you. Next time, please use the tools explained on this page to make your question/comment more readable.
Rik, Thank you very much for editting, and sorry for that.
Guillaume
Guillaume 2019년 6월 2일
You've already given us the code in your question. As I said, it's very fragile. What I asked for is an example file to be parsed
Rik
Rik 2019년 6월 3일
@Guillaume the data inside the file is at the bottom of his comment. It is unclear to me however if there really are no newline characters in that file, or if the were just omited when posting here.
@Mohammed Can just just attach the file? You will have to put it in a zip first if it is not an otherwise supported file format. Then we can read the file ourselves and help you write more robust code.
Stephen23
Stephen23 2019년 6월 3일
Mohammed Qahosh 's "Answer" moved here:
These are the data from a file. There would be 2 spaces seperating each line, if all lines were in one line. I got this form once I copied them and pasted in matlab.
PCO Pixelfly USB Image
FileName: D:\experiment\MoleculeExp\FEM\Video\v190322ae\v190322ae_0.pco
File_version = 1
Date = 22.3.2019
Time = 15:26:42
Height = 600
Width = 800
Binning = 1
Exposure Time = 1000 msec
Average = 5
Temperature = 45
Pixel Rate = 12
Tip Voltage = 2150
Shutter State = 0
End
Rik
Rik 2019년 6월 3일
편집: Rik 2019년 6월 3일
Please attach the file itself, not a text copy. Use the paperclip icon to attach it to a comment.
Dear Rik, thank you very much for your help, and sorry for my late response I was so busy.
Actually, I added the following code to the original one and it works :
A=regexp(text,'Time');
B=regexp(text,'Height');
L=B-A(1);
if 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_hour=str2num(text(C(1)+13:strfind(text,'Height')-3));
time=60*60*time_hour+60*time_min+time_sec;
end
% % I also changed the length L based on the time and it worked

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2019년 5월 30일

댓글:

2019년 6월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by