필터 지우기
필터 지우기

Read time with colon from .txt file

조회 수: 5 (최근 30일)
Hendrik
Hendrik 2013년 12월 5일
편집: per isakson 2013년 12월 6일
Hello, I have an input file with data that contains the time, but it is in the format of the current time, thus 12:25:25.17 for example (12 hours, 25 minutes, 25 seconds and 17 miliseconds) but when matlab tries to read the column of data, it only takes the 12, not the rest. So i tried to replace all : for a space and wanted to convert the hours hours, minutes and seconds to miliseconds so that you get one column that matlab can read. however when i tried to replace the : in the txt file, the output is only shown in the command window. I cant figure out how to save it in an array (so making from 1 array 4 arrays). Or if there is an easier way for matlab to read these numbers containing several : each, that would be handy.

채택된 답변

per isakson
per isakson 2013년 12월 5일
편집: per isakson 2013년 12월 6일
Try
fid = fopen( 'data.txt' );
cac = textscan( fid, '%s%f%f%f%f', 'CollectOutput', true );
sts = fclose( fid );
sdn = datenum( char(cac{1}), 'HH:MM:SS.FFF' );
vec = datevec( char(cac{1}), 'HH:MM:SS.FFF' );
where data.txt contains
12:25:25.17 1 2 3 4
12:25:26.99 5 6 7 8
Note:
Above, char(cac{1}) may be replaced by cac{1}, since Doc (R2013a) on datevec says:
DateVector = datevec(DateString,formatIn)
and
DateString — Date stringsstring | cell array of strings
  댓글 수: 2
Hendrik
Hendrik 2013년 12월 5일
Thanks, this works perfectly,
Although i am not sure what the numbers in column 1, 2 and 3 from the array vec mean. (But that does not matter because the other 3 columns give everything i need.) Thanks again.
per isakson
per isakson 2013년 12월 5일
"column 1, 2 and 3 from" are default values for year, month and day. See the help.

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

추가 답변 (1개)

dpb
dpb 2013년 12월 5일
>> s='12:25:25.17'
>> fmt=[repmat('%d:',1,2) '%d.%d'];
>> sscanf(s,fmt)
ans =
12
25
25
17
>>
Salt the format to suit the full record, of course. And, if you need yet more flexibility,
doc textscan
  댓글 수: 2
Hendrik
Hendrik 2013년 12월 5일
Your method will work if you can import the full numbers including the colons to matlab. But the problem is that these numbers are in the .txt file and in matlab only takes the hours. If i try
Data = load('data.txt')
time = Data(:,1)
Then it will only show time(1)=12, time(2)=12, time(3)=12 etc. So for it to work, i would need to add a ' before and after the entire first column in the .txt file.
dpb
dpb 2013년 12월 5일
doc textscan
doc fscanf
Use fopen() to open the file, I simply demonstrated the format string to handle the time format.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by