Problem with "datenum" and "datetime" functions

7 ビュー (過去 30 日間)
Peter Valent
Peter Valent 2019 年 4 月 29 日
編集済み: Shubhra S 2021 年 9 月 8 日
I need to extract date information from a formated date string. I have used both datenum and datetime functions but both of them returned an error. However, when I run the code on a different computer it worked well. Here is the simple code and the corresponding error:
Example for DATENUM:
datenum('11/02/2005 13:10:00','dd/mm/yyyy HH:MM:SS')
datenum('11/02/2005 13:10:00','dd/mm/yyyy HH:MM:SS')
Error: Invalid text character. Check for unsupported symbol,
invisible character, or pasting of non-ASCII characters.
Example for DATETIME:
datetime('11/02/2005 13:10:00','InputFormat','dd/MM/yyyy HH:mm:ss')
datetime('11.02.2005 13:10:00','InputFormat','dd.MM.yyyy HH:mm:ss')
Error: Invalid text character. Check for unsupported symbol,
invisible character, or pasting of non-ASCII characters.
Why don't the functions work on my computer?
  3 件のコメント
Peter Valent
Peter Valent 2019 年 4 月 29 日
@Stephen Cobeldick: I think that no example script is neccesary. When I copy the two lines of code given in the question I get the error on one computer but no error at another one.
The specs of the PC where it throws error are: WIN10, MATLAB R2018b - academic use.
The specs of the PC where it does not throw error are: WIN10, MATLAB R2017a - academic use.
Moreover, the following code also doesn't work on the first PC and works on the second PC:
sscanf('10/10/10 10:10:10','%d/%d/%d %d:%d:%d')
Shubhra S
Shubhra S 2021 年 9 月 8 日
編集済み: Shubhra S 2021 年 9 月 8 日
I am also facing a similar error.The function works on one pc and doesn't work on another

サインインしてコメントする。

採用された回答

Sean de Wolski
Sean de Wolski 2019 年 4 月 29 日
I was able to reproduce this by copying your code from above. It appears the opening ( is bad (right where the arrow is)
datetime('11/02/2005 13:10:00','InputFormat','dd/MM/yyyy HH:mm:ss')
Deleting your ( and replacing with my ( fixed it.
Further investigating it looks like there is an invisible character after the (
int32('(')
ans =
1×2 int32 row vector
40 65279
  1 件のコメント
Peter Valent
Peter Valent 2019 年 4 月 30 日
I have addet the following check, which solved the problem.
textToConvert = '11/02/2005 13:10:00';
acceptedCharacters = int32([' ','.','/',int32(arrayfun(@num2str,0:9)),':']);
textToConvert = textToConvert(ismember(int32(textToConvert),acceptedCharacters));
datenum(textToConvert,'dd/mm/yyyy HH:MM:SS')

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by