Definitive answer on the correct str2double behavior for the string '10 20'

1 回表示 (過去 30 日間)
Nicholas
Nicholas 2014 年 7 月 9 日
コメント済み: Nicholas 2014 年 7 月 10 日
Under Windows 7 with R2014a, str2double('10 20') evaluates to NaN. I believe this is the correct behavior, per the documentation.
A colleague working in a Linux environment with R2014a reports that str2double('10 20') evaluates to an array [10 20].
1. Is this possible (without overloading)? 2. Am I correct that the intended evaluation (without overloading) is NaN?
Thanks so much.

採用された回答

per isakson
per isakson 2014 年 7 月 10 日
編集済み: per isakson 2014 年 7 月 10 日
"str2double('10 20') evaluates to an array [10 20]." . If that is the case it should be reported as a bug
  1. It should not be possible without overloading
  2. You are correct
I advise against unrestricted use of str2num. It uses eval and ... , e.g.
>> str2num( 'sin([0:0.8:2*pi])')
ans =
0 0.7174 0.9996 0.6755 -0.0584 -0.7568 -0.9962 -0.6313
IMO: sscanf is a better alternative
>> vec = sscanf( '10 20', '%f' )
vec =
10
20

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 9 日
編集済み: Azzi Abdelmalek 2014 年 7 月 9 日
str2num('10 20')
will give an array, maybe that's what your colleague has used
  1 件のコメント
Nicholas
Nicholas 2014 年 7 月 10 日
Thanks Azzi. Yes, we ended up changing the code to use str2num, to give OS independent results. But I am still curious about: a) the definitive (Mathworks intended) behavior of str2double; and b) whether OS can in fact affect behavior (I don't have a Linux machine here to investigate further).

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by