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

Barry Swindler
Barry Swindler 2016년 5월 30일
편집: Barry Swindler 2016년 5월 30일
not sure if you found an answer yet, but a simple thing like this works.
m=zeros(size(a,1),size(a,2));
m=str2double(a);
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
Jan 2024년 11월 13일
편집: MathWorks Support Team 2024년 11월 13일

36 개 추천

이 답변에 Walter Roberson 님이 플래그를 지정함
  • Walter Roberson 님이 2025년 12월 13일에 플래그를 지정했습니다.

    needs reformatting

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

Daniel Shub
Daniel Shub 2011년 10월 17일
Nice comparison. One of the benefits of my job is access to way more computational power than I need. In general, I rarely worry about efficiency, however a speed up of a factor of 50 is nothing to sneeze at.
Daniel Shub
Daniel Shub 2011년 10월 17일
편집: Jan 2025년 12월 13일
A faster, although not necessarily viable way, is to have the cell array contain numbers and not strings. On my computer
for iC = 1:n; C{iC} = iC; end
tic; for i=1:1000; N = [C{:}]; end; toc
appears fastest, although not by as much as I would have guessed.
Fangjun Jiang
Fangjun Jiang 2011년 10월 17일
+1, Nice work, Jan! str2double() has source code available. It uses sscanf().
Jan
Jan 2011년 10월 18일
편집: Jan 2015년 5월 5일
@Daniel: Even [C{:}] seems to have problems with the pre-allocation. The C-Mex http://www.mathworks.com/matlabcentral/fileexchange/28916-cell2vec determines the needed output size at first. For the {1 x100} cell containing scalars it is 5 times faster than [C{:}] in 2001b.
Jan
Jan 2011년 10월 18일
Inserted: N(j)=sscanf(C{j}, '%f').
hello_world
hello_world 2018년 7월 4일
편집: Walter Roberson 2025년 12월 13일
@Jan great example, but it does not help if the cell contains strings. I have raised a question with that problem at: https://www.mathworks.com/matlabcentral/answers/408675-how-to-convert-a-csv-file-of-cell-array-type-to-double
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?
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021년 6월 28일
Dear Jan,
thanks for your tip. Still useful these days.
Best.
Hiril Patel
Hiril Patel 2022년 6월 19일
Hello Jan
i have cell of 73x1 of splitted data which i would like to convert to decimal but when I use M = str2double(adc_data). it returns to NaN
here is the string data i splitted
Christopher Saltonstall
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])
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
Daniel Shub 2011년 10월 17일

6 개 추천

It appears your cell array contains strings and not numbers (doubles).
b = cellfun(@(x)str2double(x), a);

댓글 수: 6

Fangjun Jiang
Fangjun Jiang 2011년 10월 17일
Believe it or not, you can use str2double directly.
a={'1','2'};
str2double(a)
Daniel Shub
Daniel Shub 2011년 10월 17일
Wow, another reason to like str2double over str2num.
Jan
Jan 2011년 10월 17일
cellfun(@str2double, C) is faster than str2double(C). Surprising!
Jan
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
hello_world 2018년 7월 4일
편집: Walter Roberson 2025년 12월 13일
@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?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2011년 10월 17일

편집:

2025년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by