MATLAB Answers

Cell conversion to double

4,885 ビュー (過去 30 日間)
Steven 2011 年 10 月 17 日
コメント済み: Nishaban P.K. 2020 年 4 月 21 日
Let's say a is a 11x1 cell like this
a =
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)));
Thus, I wonder if there is a simpler way to do this with an easy one-step function.

  3 件のコメント

Barry Swindler
Barry Swindler 2016 年 5 月 30 日
not sure if you found an answer yet, but a simple thing like this works.
still not just one step, but there's no need for a loop.
hope this helps!
ayesha abbassi
ayesha abbassi 2018 年 2 月 24 日
B = cell2mat(A). now check its type by writing whos B in command window
Chrysi K.
Chrysi K. 2019 年 2 月 5 日
@ayesha abbassi Thank you so much!!! You helped me!!! I had a similar problem!



Jan 2011 年 10 月 17 日
編集済み: MathWorks Support Team 2018 年 11 月 8 日
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: FEX: CStr2String:
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 FEX: String to double. Matlab's sscanf seems to be much better than the MSVC implementation.

  7 件のコメント

表示 4 件の古いコメント
Jan 2011 年 10 月 18 日
Inserted: N(j)=sscanf(C{j}, '%f').
hello_world 2018 年 7 月 4 日
@Jan great example, but it does not help if the cell contains strings. I have raised a question with that problem at:
Can you please help with that?
Nishaban P.K.
Nishaban P.K. 2020 年 4 月 21 日
sir, I use str2double function ,it return some binary values like '00000111' into 111. why?


その他の回答 (1 件)

Daniel Shub
Daniel Shub 2011 年 10 月 17 日
It appears your cell array contains strings and not numbers (doubles).
b = cellfun(@(x)str2double(x), a);

  6 件のコメント

表示 3 件の古いコメント
Jan 2011 年 10 月 17 日
"cellfun(@str2double, C)" is faster than "str2double(C)". Surprising! But the indirection "cellfun(@(x)str2double(x), C)" wastes time.
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 17 日
+1, For none-time-critical task, I still vote for using str2double().
hello_world 2018 年 7 月 4 日
@Daniel Shub: It converts all cell entries (which are string) into NaN values. I have raised a question at:
Can you please look into that?


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by