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 › matlabcentral › answers › 478723-how-to-convert-array-of-strings-into-array-of-double-or-int
how to convert array of strings into array of double or int - MATLAB Answers - MATLAB Central
September 3, 2019 - hello everyone, I want to convert an array of string which has numbers into an array of numbers (double). The char array i want to convert is : ...
🌐
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.
Find elsewhere
🌐
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...
🌐
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
Top answer
1 of 1
2
The reason for the observed behavior is the stdio library used by Arduino. The code generated by str2double uses a function called sscanf. This function internally calls vfscanf function. vfscanf is available in three different versions. Out of these three versions, the default version does not implement scanning float variables. This is mentioned in the doc: "By default, all the conversions described above are available except the floating-point conversions and the width is limited to 255 characters. The float-point conversion will be available in the extended version provided by the library libscanf_flt.a. Also, in this case the width is not limited (exactly, it is limited to 65535 characters). To link a program against the extended version, use the following compiler flags in the link stage: -Wl,-u,vfscanf -lscanf_flt -lm " Refer the following doc link for more details: https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#ga67bae1ad3af79809fd770be392f90e21 To use compiler flags mentioned in the doc, you can follow the steps mentioned below: 1. Open config set 2. Go to "Code Generation" tab 3. In "Build configuration" drop down, select "Specify" 4. In C++ Liker, append the following: -Wl,-u,vfscanf -lscanf_flt -lm 5. Dont forget to add a space to the existing flags before appending the new flags. 6. Click apply, OK. 7. Run the model in external mode. Please note that using the extended version of this library will consume a lot of code space.