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!