Cell conversion to double
2,216 次查看(过去 30 天)
显示 更早的评论
Greetings, Let's say a is a 11x1 cell like this a = '0.000000' '1.000000' '2.000000' '3.000000' '4.000000' '5.000000' '6.000000' '7.000000' '8.000000' '9.000000' '10.000000' and I want to convert it in double. If I try b=cell2mat(a), I got the following error : ??? Error using ==> cat CAT arguments dimensions are not consistent. Error in ==> cell2mat at 85 m{n} = cat(1,c{:,n}); However, I know I can bypass it with a loop with 2 conversion as: for i = 1:length(a) b(i) = str2num(cell2mat(a(i))); end Thus, I wonder if there is a simpler way to do this with an easy one-step function. Regards, Steven
3 个评论
采纳的回答
Jan
2024-11-13
编辑:MathWorks Support Team
2024-11-13
此 个回答 已被 Walter Roberson
标记
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.
11 个评论
Christopher Saltonstall
2024-12-12
编辑:Christopher Saltonstall
2024-12-13
@Jan I have a similar problem but a slightly different data set. My data set is read from a strangly formated .csv file an yields a table where each cell of the table has an array. When I read one cell of the table, I get something like this but much longer.
C = {'[0 1 2 3 4]'};
How can I convert this to a double array?
This is the only way that I have figured out how to do this but eval does not work with my data size.
A = cell2mat(C)
eval(['data = ' A])
Walter Roberson
2024-12-13
A = cellfun(@(S)str2num(S,Evaluation="restricted"), TableNameGoesHere.FieldNameGoesHere, 'uniform', 0)
possibly followed by a cell2mat(A) provided that all of the entries are the same size.
更多回答(1 个)
Daniel Shub
2011-10-17
It appears your cell array contains strings and not numbers (doubles).
b = cellfun(@(x)str2double(x), a);
6 个评论
hello_world
2018-7-4
@Daniel Shub: It converts all cell entries (which are string) into NaN values. I have raised a question at: https://www.mathworks.com/matlabcentral/answers/408675-how-to-convert-a-csv-file-of-cell-array-type-to-double
Can you please look into that?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!