Why does assigning a string object into a double vector of doubles give NaN instead of an error?
조회 수: 12 (최근 30일)
이전 댓글 표시
I noticed that assing a string object to a double array, for example,
a=0
a(1)="asd"
does not produces an error but sets the value of a(1) to NaN;
This seems inconsistent with assinging an incompatible type like for example a cell array, e.g.
a=0
a(1)={'asd'}
---> Conversion to double from cell is not possible.
I would prefer a string assignment to double to also raise an error. Any ideas about why it does not?
댓글 수: 0
채택된 답변
Steven Lord
2020년 7월 7일
편집: Steven Lord
2020년 7월 7일
MATLAB knows how to convert double arrays to string arrays and vice versa. For the former see this documentation page, for the latter see the "Create, Concatenate, and Convert" section on this documentation page. But if the string is not a text representation of a number, the double value of the string is NaN.
>> d1 = 0:5;
>> s1 = string(d1)
s1 =
1×6 string array
"0" "1" "2" "3" "4" "5"
>> d2 = double(s1)
d2 =
0 1 2 3 4 5
>> d3 = double("abracadabra")
d3 =
NaN
MATLAB does not know how to convert a cell array to double.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!