str2num(chr) converts a character array or string scalar to a numeric matrix. The input can include spaces, commas, and semicolons to indicate separate elements. If str2num cannot parse the input as numeric values, then it returns an empty matrix.
The str2num function does not convert cell arrays or nonscalar string arrays, and is sensitive to spacing around + and - operators.
str2num() contains a call to eval(), which means that if your string has the same form as an array (e.g. with semicolons) then it is simply executed and the resulting array is returned. The problems with str2num() are that it doesn’t support cell arrays, and that because it uses an eval() function, wierd things can happen if your string includes a function call. str2double() is supposedly faster as well. So the general rule seems to be, use str2double() unless you are wanting to convert a string array of numbers to a numerical array.
There is one catch though- if you are converting a single number from a string to a number, then it would SEEM like str2num() and str2double() are interchangable, drop in replacements. However, if the string is not a number, they behave differently Answer from Vignesh Murugavel on mathworks.com
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
str2double - Convert strings to double precision values - MATLAB
X = str2double(str) converts the text in str to double precision values. str contains text that represents real or complex numeric values. str can be a character vector, a cell array of character vectors, or a string array. If str is a character vector or string scalar, then X is a numeric scalar.
MathWorks
mathworks.com › requirements toolbox › author and validate requirements › model and validate requirements
str2double - Convert string to double-precision value in Requirements Table block - MATLAB
X = str2double(str) converts the text in string str to a double-precision complex value.
Videos
MATLAB: String Functions - str2double/num2str (7 of 9) - YouTube
00:24
MATLAB: str2num (convert strings to numbers - double data type) ...
00:35
MATLAB: str2double (convert strings to numbers - double data type) ...
00:27
MATLAB: num2str (convert numbers/double data type to strings) - ...
MathWorks
mathworks.com › matlabcentral › answers › 1812775-convert-char-to-double
convert char to double - MATLAB Answers - MATLAB Central
September 27, 2022 - var = '0' (this is char) and i want this value convert 'double' so i used out = str2double(regexp(var,'\d*','match') But I have a parse error How can I get the val value in double? For r...
MathWorks
mathworks.com › stateflow › simulation in simulink › data specification › string data
str2double - Convert string to double-precision value in Stateflow chart - MATLAB
X = str2double(str) converts the text in string str to a double-precision value. In a chart that uses MATLAB® as the action language, str2double returns a complex value.
MATLAB
it.mathworks.com › matlabcentral › answers › 309083-str2double-conversion
str2double conversion... - MATLAB Answers
October 25, 2016 - View questions and answers from the MATLAB Central community. Find detailed answers to questions about coding, structures, functions, applications and libraries.
MathWorks
mathworks.com › matlabcentral › answers › 489754-cell-array-data-to-double-conversion
Cell array data to double conversion - MATLAB Answers - MATLAB Central
November 7, 2019 - https://www.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#answer_400338 ... Assuming each cell has only one entry, try using cellfun with str2double to convert the cell array of strings to a cell array of doubles, then use cell2mat.
MathWorks
mathworks.com › matlabcentral › answers › 428432-why-am-i-returning-nan-with-str2double
Why am I returning NaN with str2double? - MATLAB Answers - MATLAB Central
November 7, 2018 - This doesn't match the behavior of any programming language I'm familar with. Thus, you need to check the type of x before calling str2double. Worse yet, Matlab doesn't have a ternary operator, so this seemingly simple problem becomes a real headache due to poor Matlab design choices.
MathWorks
mathworks.com › matlabcentral › answers › 321211-how-to-use-str2double-without-rounding
How to use str2double without rounding - MATLAB Answers - MATLAB Central
January 20, 2017 - I am using the str2double function and it always rounds the conversion to the 4 places after the decimal point. Do you guys have any idea on converting the whole string exactly the way it is without rounding ?? ... https://www.mathworks.com/matlabcentral/answers/321211-how-to-use-str2double-without-rounding#comment_421778
MathWorks
mathworks.com › matlabcentral › answers › 657783-difference-between-str2num-and-str2double-for-getting-a-matrix
difference between str2num and str2double for getting a matrix - MATLAB Answers - MATLAB Central
November 22, 2020 - >> txt = '5 7; 8 9'; >> f = str2num(txt) f = 5 7 8 9 >> f = str2double(txt) f = NaN So I have a text of that form with semi-colon and spaces and the str2...
Top answer 1 of 2
1
str2double will only convert text that represents real or complex scalar values. Where y is a char array of binary values. It is basically interpreting y as one large integer. Hence, it will return NaN or Inf depending on the version of MATLAB you are using.
2 of 2
0
You can use convertCharsToStrings and then use str2double
e.g
for i = 1:length(y)
tempvar = convertCharsToStrings(y(i,:));
x1(i) = str2double(tempvar);
end
OR if you just want to convert all string into double then use
arrayfun(@(x)str2double(convertCharsToStrings(x)),y,'Uniformoutput',false)
MathWorks
mathworks.com › matlabcentral › answers › 1892930-convert-multiple-strings-in-a-table-to-numbers
Convert multiple strings in a table to numbers - MATLAB Answers - MATLAB Central
January 12, 2023 - Table.(: ,X:YY) = str2double(Table.(: ,X:YY)); doesn't seem to work, as it returns an error, and changing the () to {} returns a NaN. Anybody that knows how I can easily change string to double for multiple columns in a table? Sign in to comment. Sign in to answer this question. Share · Follow Question · Stephen23 on 12 Jan 2023 · Ran in: Open in MATLAB Online ·
Northwestern University
ece.northwestern.edu › local-apps › matlabhelp › techdoc › ref › str2double.html
str2double (MATLAB Functions)
X = str2double('str') converts the string str, which should be an ASCII character representation of a real or complex scalar value, to the MATLAB double-precision representation.
MathWorks
mathworks.com › matlabcentral › answers › 337767-how-to-convert-from-cell-with-letters-to-double
How to convert from cell with letters to double? - MATLAB Answers - MATLAB Central
April 28, 2017 - https://www.mathworks.com/matlabcentral/answers/337767-how-to-convert-from-cell-with-letters-to-double#answer_264875 ... newv = str2double(regexp(v, '\d+', 'match', 'once')) %this regex extracts the integer from the string
MathWorks
mathworks.com › matlabcentral › answers › 782193-converting-string-to-a-double
Converting string to a double - MATLAB Answers - MATLAB Central
March 24, 2021 - In order to create the graph I had to convert the strings in the txt file to a double, but the str2double function rounds the values so the graph isn't accurate. for example, the string to convert is: 1.7356922617E-3, and the doublue value after applying the method is 1.73569E-3. is there any method that convert without rounding? ... https://www.mathworks.com/matlabcentral/answers/782193-converting-string-to-a-double#comment_1414313