Not able to convert string to seconds! Even though I am using datenum function. How can it be solved?
6 ビュー (過去 30 日間)
古いコメントを表示
I have been trying to convert time string from a large datal, but it Matlab gives me "Unrecognized millisecond format. Format string: HH:MM:SS.FFFF."
And I would like to get the data immediately from the file instead of importing them.
The format of the string is
12:10:02.908990
Here is the code:
t1;
datenum(t1,'HH:MM:SS.FFFF')
0 件のコメント
回答 (2 件)
masoud sistaninejad
2023 年 7 月 30 日
in MATLAB R202a and later you can use
datetime command like this ...
t1 = '12:10:02.908990'
datetime(t1)
0 件のコメント
Walter Roberson
2023 年 7 月 30 日
編集済み: Walter Roberson
2023 年 7 月 30 日
datenum() only supports up to millisecond resolution -- so at most FFF .
Also, your input format needs to account for the entire input string. You cannot just specify FFF and expect it to stop parsing after it reads 3 digits of milliseconds.
t1 = '12:10:02.908990';
d = duration(t1, 'InputFormat', 'hh:mm:ss.SSSSSS', 'format','hh:mm:ss.SSSSSS')
datenum_you_were_looking_for = seconds(d) / (24 * 60 * 60)
ds = datestr(datenum_you_were_looking_for, 31) %cross-checking
I don't think the number of days since the beginning of "0 AD" is going to be useful, especially with the lost resolution (because datenum just do not have the resolution)- You should probably be seriously considering getting rid of using datenum()
3 件のコメント
Stephen23
2023 年 7 月 30 日
編集済み: Stephen23
2023 年 7 月 30 日
"What are other alternatives to datenum()? "
Lets read the DATENUM documentation and see what it tells us:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1445547/image.png)
The DURATION class would suit your input data (times given as hours, minutes, seconds + fraction of second). Note the DURATION class has properties and methods that you will find useful.
Walter Roberson
2023 年 7 月 30 日
datetime and duration classes have the resolution needed, but their default Format does not include full resolution for display purposes.
参考
カテゴリ
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!