Strange behaviour using double

I'm currently using Matlab 2020b
The doumentation for the double function states that 'Converting a string that does not represent a single numeric value to double will produce a NaN result'. Based on this, I am using isnan(double(string('chars'))) to check whether a user inputted character vector represents a number or not. One particular case I wish to guard against is users entering a ',' instaed of '.'. This works very well, except if the vector in question has a comma followed by exactly 3 numerals, in which case double tells me it is a number. For example, the vector '1.23' will produce NaN, but '1,234' gives the number 1234. Any other number of characters after the comma gives NaN (as far as I can tell).
Does anybody understand why this is and is there a way to get it to do what I actually want?
Thanks

4 件のコメント

Torsten
Torsten 2025 年 9 月 10 日
Can you include the exact code that produces these results ?
Matt J
Matt J 2025 年 9 月 10 日
For example, the vector '1.23' will produce NaN
No, it won't. You probably meant something else.
double(string('1.23'))
ans = 1.2300
isnan(ans)
ans = logical
0
Stephen23
Stephen23 2025 年 9 月 10 日
"For example, the vector '1.23' will produce NaN"
From the context we can assume that the example uses a comma:
double("1,23")
ans = NaN
dpb
dpb 2025 年 9 月 11 日
編集済み: dpb 2025 年 9 月 11 日
How do you know the user didn't intend to enter "1,234" as the value 1234?
Is there instruction that valid MATLAB numeric optional thousands separators are not allowed in this application?
Where is the user input coming from? If interactive, you could set the uieditfield to be 'numeric' instead of 'text' and MATLAB will inform the user when tries to enter a nonallowable character.

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

 採用された回答

Stephen23
Stephen23 2025 年 9 月 10 日
編集済み: Stephen23 2025 年 9 月 11 日

1 投票

"Does anybody understand why this is"
The comma is an optional thousands delimiter in english: https://en.wikipedia.org/wiki/Decimal_separator#Digit_grouping
" and is there a way to get it to do what I actually want?"
Nope, accepting optional thousands delimiters is what STR2DOUBLE and DOUBLE (likely for compatibility reasons) both do. There is nothing stopping you from modifying its input data, e.g. replacing the comma with '@'.

その他の回答 (1 件)

Matt J
Matt J 2025 年 9 月 10 日
編集済み: Matt J 2025 年 9 月 10 日

0 投票

You could replace the commas with some other NaN-triggering character, e.g.,
c={'1.23', '1,234'}
c = 1×2 cell array
{'1.23'} {'1,234'}
double(replace(string(c),',','?') )
ans = 1×2
1.2300 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

1 件のコメント

Matt J
Matt J 2025 年 9 月 10 日
Or,
c={'1.23', '1,234'};
isnan(double(string(c))) | contains(c,',')
ans = 1×2 logical array
0 1

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2020b

タグ

質問済み:

2025 年 9 月 10 日

編集済み:

dpb
2025 年 9 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by