Cell conversion to double
이전 댓글 표시
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
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
2018년 2월 24일
B = cell2mat(A). now check its type by writing whos B in command window
Chrysi K.
2019년 2월 5일
@ayesha abbassi Thank you so much!!! You helped me!!! I had a similar problem!
채택된 답변
추가 답변 (1개)
Daniel Shub
2011년 10월 17일
It appears your cell array contains strings and not numbers (doubles).
b = cellfun(@(x)str2double(x), a);
댓글 수: 6
Fangjun Jiang
2011년 10월 17일
Believe it or not, you can use str2double directly.
a={'1','2'};
str2double(a)
Daniel Shub
2011년 10월 17일
Wow, another reason to like str2double over str2num.
Jan
2011년 10월 17일
cellfun(@str2double, C) is faster than str2double(C). Surprising!
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
2011년 10월 17일
+1, For none-time-critical task, I still vote for using str2double().
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!