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.
Videos
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 ·
MathWorks
mathworks.com › matlabcentral › answers › 650478-how-to-convert-cell-to-number-in-matlab
how to convert cell to number in matlab - MATLAB Answers - MATLAB Central
November 17, 2020 - how to convert cell to number in matlab. Learn more about str2double
Top answer 1 of 4
4
If you convert your matrix into a column vector, each character will be positioned in a different row, so that it can be handled independently by str2num. Then reshape back:
result = reshape(str2num(A(:)), size(A))
2 of 4
0
I don't know why you are converting twice. Just go through both indexes and convert each char and put it in the new matrix.
for i = 1:size(A,1)
for t = 1:size(A,2)
result(i,t) = str2num(A(i,t));
end
end
PS: Preallocating result befoor the loops will increase speed:
result = zeros(size(A))
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 › fixed-point designer › data type exploration › fixed-point specification › fixed-point specification in matlab › fixed-point math functions
bin2num - Convert two's complement binary string to number using quantizer object - MATLAB
This MATLAB function converts the binary character vector b to a numeric array y using the properties of the quantizer object q.
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.
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.