Read time with colon from .txt file
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
per isakson
2013-12-5
"column 1, 2 and 3 from" are default values for year, month and day. See the help.
更多回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!