To convert a cell array of character vectors to numbers, you can use the |str2double| function. This function is the simplest method. C = {'0.000000'; '10.000000'; '100000.000000'}; M = str2double(C) The |cell2mat| function converts a cell array of character vectors to a character array, and only if all the character vectors have the same length. |cell2mat| also preserves the data type of the contents of the cells, so it does not convert characters to numbers. If you need your code to be fast, then use the following code instead. This code is faster than |str2double|: C = {'0.000000'; '1.000000'; '2.000000'; ... '3.000000'; '4.000000'; '5.000000'; '6.000000' '7.000000'; '8.000000'; '9.000000'; '10.000000'}; S = sprintf('%s*', C{:}); N = sscanf(S, '%f*'); Unfortunately |sprintf| seems to forget a proper pre-allocation. This C-Mex is 4 times faster: : S = CStr2String(C, '*'); N = sscanf(S, '%f*'); Timings in 2011b, Core2Duo: n = 100; C = cell(1, n); for iC = 1:n; C{i} = sprintf('%f', i); end tic; for i=1:1000; N = cellfun(@(x)str2double(x), C); end; toc >> 3.61 sec tic; for i=1:1000; N = cellfun(@(x) sscanf(x, '%f'), C); end; toc >> 3.01 sec tic; for i=1:1000; N = str2double(C); end; toc >> 2.79 sec tic; for i=1:1000; N = cellfun(@str2double, C); end; toc >> 2.49 sec tic; for i=1:1000; N = zeros(1,100); for j=1:100; N(j) = sscanf(C{j}, '%f'); end; end; toc >> 1.40 sec tic; for i=1:1000; N = sscanf(sprintf('%s*', C{:}), '%f*'); end; toc >> 0.14 sec tic; for i=1:1000; N = sscanf(CStr2String(C, '*'), '%f*'); end; toc >> 0.071 sec To my surprise a full implementation in C is *slower* than |sscanf(sprintf())|, see . Matlab's sscanf seems to be much better than the MSVC implementation. Answer from Jan on mathworks.com
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
str2num - Convert character array or string to numeric array - MATLAB
X = str2num(txt) converts a character array or string scalar to a numeric matrix. The input can include spaces, commas, and semicolons to indicate separate elements.
Top answer
1 of 1
36
To convert a cell array of character vectors to numbers, you can use the |str2double| function. This function is the simplest method. C = {'0.000000'; '10.000000'; '100000.000000'}; M = str2double(C) The |cell2mat| function converts a cell array of character vectors to a character array, and only if all the character vectors have the same length. |cell2mat| also preserves the data type of the contents of the cells, so it does not convert characters to numbers. If you need your code to be fast, then use the following code instead. This code is faster than |str2double|: C = {'0.000000'; '1.000000'; '2.000000'; ... '3.000000'; '4.000000'; '5.000000'; '6.000000' '7.000000'; '8.000000'; '9.000000'; '10.000000'}; S = sprintf('%s*', C{:}); N = sscanf(S, '%f*'); Unfortunately |sprintf| seems to forget a proper pre-allocation. This C-Mex is 4 times faster: : S = CStr2String(C, '*'); N = sscanf(S, '%f*'); Timings in 2011b, Core2Duo: n = 100; C = cell(1, n); for iC = 1:n; C{i} = sprintf('%f', i); end tic; for i=1:1000; N = cellfun(@(x)str2double(x), C); end; toc >> 3.61 sec tic; for i=1:1000; N = cellfun(@(x) sscanf(x, '%f'), C); end; toc >> 3.01 sec tic; for i=1:1000; N = str2double(C); end; toc >> 2.79 sec tic; for i=1:1000; N = cellfun(@str2double, C); end; toc >> 2.49 sec tic; for i=1:1000; N = zeros(1,100); for j=1:100; N(j) = sscanf(C{j}, '%f'); end; end; toc >> 1.40 sec tic; for i=1:1000; N = sscanf(sprintf('%s*', C{:}), '%f*'); end; toc >> 0.14 sec tic; for i=1:1000; N = sscanf(CStr2String(C, '*'), '%f*'); end; toc >> 0.071 sec To my surprise a full implementation in C is *slower* than |sscanf(sprintf())|, see . Matlab's sscanf seems to be much better than the MSVC implementation.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 515357-converting-string-to-number-with-spaces
Converting string to number with spaces - MATLAB Answers - MATLAB Central
April 4, 2020 - https://www.mathworks.com/matlabcentral/answers/515357-converting-string-to-number-with-spaces#answer_424007 ... Because your string is so long, str2num will not work.
🌐
Omics
matlab.omics.wiki › matrix › cell-array › str2num
Matlab by Examples - str2num
Matlab by Examples · a) convert numerical cell array to matrix · C={ [1],[2],[3]; [4],[5],[6] } [1] [2] [3] [4] [5] [6] cell2mat(C) 1 2 3 · 4 5 6 · b) convert text string cell array to matrix · C={ '1','2','3' ; '4','5','6' } '1' '2' '3' '4' '5' '6' cellfun(@str2double,C) 1 2 3 ·
Find elsewhere
🌐
MathWorks
mathworks.com › matlabcentral › answers › 114597-str2num-not-working-on-bigger-arrays
str2num() not working on bigger arrays? - MATLAB Answers - MATLAB Central
February 2, 2014 - You should be considering using str2double() instead of str2num(). str2num() requires that the strings be executed as commands. Note: str2double() works on a char vector or a cell array of strings, but not on a char array. ... https://www.mathworks.com/matlabcentral/answers/114597-str2num-not-working-on-bigger-arrays#comment_194318
🌐
Johns Hopkins University
math.jhu.edu › ~shiffman › 370 › help › techdoc › ref › str2num.html
str2num (MATLAB Function Reference)
Description x = str2num('str') converts the string str, which is an ASCII character representation of a numeric value, to MATLAB's numeric representation.
🌐
MathWorks
mathworks.com › fixed-point designer › data type exploration › fixed-point specification › fixed-point specification in matlab › fixed-point math functions
hex2num - Convert hexadecimal string to number using quantizer object - MATLAB
This function uses a quantizer object to convert a hexadecimal string to a number. To convert IEEE hexadecimal format to a double-precision number without using a quantizer object, use the MATLAB® hex2num function.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
Convert Text to Numeric Values - MATLAB & Simulink
While the str2num function can also convert text to numbers, it is not recommended. str2num uses the eval function, which can cause unintended side effects when the text input includes a function name.
🌐
R Project
search.r-project.org › CRAN › refmans › pracma › html › str2num.html
R: Converting string to number (Matlab style)
str2num converts a string containing numbers into a numerical object. The string can begin and end with '[' and ']', numbers can be separated with blanks or commas; a semicolon within the brackets indicates a new row for matrix input.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 508324-converting-a-string-into-a-numerical-array
Converting a string into a numerical array - MATLAB Answers - MATLAB Central
March 1, 2020 - https://www.mathworks.com/matlabcentral/answers/508324-converting-a-string-into-a-numerical-array#comment_804031 ... It's in the documentation... "... str2num uses the eval function, which can cause unintended side effects when the input includes a function name.
Top answer
1 of 1
2
rawStrMod = '0.0000000001, 0.3'; "It gives me 0.000000000100000, not exactly the same as 0.0000000001." Ignoring formatting issues, none of the Mathworks toolboxes are able to give you *exactly* the same as 0.0000000001 . MATLAB is not able to store 0.0000000001 *exactly* in binary floating point: the closest it can get in binary floating point is 0.00000000010000000000000000364321973154977415791655470655996396089904010295867919921875 The closest Mathworks tool can get is by using the Symbolic Toolbox, for which sym('0.0000000001') gives you something that is _closer_ to 0.0000000001 than you can get without the Symbolic Toolbox, but if you probe hard enough you can prove that sym('0.0000000001') is implemented as a binary ratio whose asymptotic accuracy is bounded by truncations of 10/3 bits per digit of accuracy requested (log2(10) would perhaps be expected but the model turned out to be 10/3: some of the bits are being used for overhead of extended precision values.) Everything else is just formatting (and perhaps keeping track of sizes). There are only a few formatting options available for default numeric display. For anything else you should be using sprintf() or fprintf() for fine detailed display. rawStrMod = '0.0000000001, 0.3, 0.0000000001000, 123'; T = regexp(rawStrMod, '\s*,\s*', 'split'); T_numeric = str2double(T); T_input_digits = cellfun(@length,regexprep(T,{'^[^.]*$', '^.*\.'},{'', ''},'once', 'lineanchors')); T_data = num2cell( [T_input_digits(:),T_numeric(:)].' ); fprintf('%.*f\n', T_data{:}); Output is 0.0000000001 0.3 0.0000000001000 123 The input was converted to numeric (T_numeric), but the number of decimal places was tracked, and that number is used to format the output. This code does have a subtle problem: * if you have an integer like the 123, then it is entirely reasonable that the output should not contain a decimal place, like happens here, with the number of decimal places being detected to be 0 * however, if the 123 were instead 123. with no digits following the decimal places, then the number of decimal places would be considered to be 0, and when told to use 0 decimal places, the %f format item does not put in the trailing decimal point, so 123. would print out as 123 with the code as implemented being unable to format 123 on input differently than 123. on input.
🌐
RDocumentation
rdocumentation.org › packages › pracma › versions › 1.9.9 › topics › str2num
str2num: Converting string to number (Matlab style)
str2num(S) num2str(A, fmt = 3) S · string containing numbers (in Matlab format). A · numerical vector or matrix. fmt · format string, or integer indicating number of decimals. Returns a vector or matrix of the same size, converted to strings, respectively numbers.