Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How to convert string "26/7" into a double variable?
조회 수: 1 (최근 30일)
이전 댓글 표시
I've tried str2double("26/7"), however it gave NaN as output.
Thanks in advance.
댓글 수: 0
답변 (3개)
John D'Errico
2020년 4월 4일
편집: John D'Errico
2020년 4월 4일
Whilst I am one of the people who tends to equate eval with evil...
str = '22/7';
eval(str)
ans =
3.14285714285714
Sometimes it can come to the rescue. Eval has a Dr. Jekyll and Mr Hyde thing about it though, so beware the dark side. ;-)
댓글 수: 6
John D'Errico
2020년 4월 4일
편집: John D'Errico
2020년 4월 4일
Yes. str2num is an option too. But then I could not have called it evil quite as easily. :)
And yes, the Jekyll and Hyde remark was something I wondered if others would catch.
Not unlike putting a reference to a slide rule in an answer these days. Most people today would look at a slide rule with a blank stare, wondering what anyone would do with it.
I did cherish my slide rule. They are mainly relegated to the antique shops now, in a sense a loss. :{
Stephen23
2020년 4월 4일
As well as being insecure eval is also slower than sscanf and rdivide, e.g. 1e5 iterations:
Elapsed time is 1.088572 seconds. % SSCAF and RDIVIDE
Elapsed time is 5.502366 seconds. % evil EVAL
KALYAN ACHARJYA
2020년 4월 4일
편집: KALYAN ACHARJYA
2020년 4월 4일
Text is "22/7" This is not a number right "/" . What do you expect at the output?
Same case:
>> X = str2double('22+7')
X =
NaN
But if we add then
>> X = str2double('29')
X =
29
댓글 수: 1
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!