What's difference between str2num and str2double for converting an array to numeric values?
显示 更早的评论
Hi, I have a 'char' array and want to convert it to a numeric array. when I use the function
str2num
I get the following error:
"Error using str2num (line 35)
Input must be a character vector or string scalar."
but I can change the format of the array using
str2double
can someone tell me why does the latter work?
5 个评论
str2num calls eval to convert a character vector to a number. That should be reason enough to avoid it.
str2double supports a wide range of container data types, but it can only convert to scalar values:
str={'1','2','1 3'}
str2double(str)
So why does str2double work for you? It must be because the specific format you use. But since you didn't post what you actually used, it is impossible to reproduce what went wrong with str2num.
ardeshir moinian
2021-8-3
编辑:ardeshir moinian
2021-8-3
Rik
2021-8-3
What you posted are char vectors, not strings. Details matter. If you want help with your actual data, you will have to post at least a working snippet of your actual data. I see no indication in what you posted why one would work, but not the other.
Walter Roberson
2021-8-3
str2num() will not work with cell array of character vectors.
str2double() will work with cell array of character vectors.
str2num() will work with char array with multiple rows.
str2double() will not work with char array with multiple rows.
ardeshir moinian
2021-8-4
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!