Cell conversion to double
Show older comments
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 Comments
Barry Swindler
on 30 May 2016
Edited: Barry Swindler
on 30 May 2016
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
on 24 Feb 2018
B = cell2mat(A). now check its type by writing whos B in command window
Chrysi K.
on 5 Feb 2019
@ayesha abbassi Thank you so much!!! You helped me!!! I had a similar problem!
Accepted Answer
More Answers (1)
Daniel Shub
on 17 Oct 2011
It appears your cell array contains strings and not numbers (doubles).
b = cellfun(@(x)str2double(x), a);
6 Comments
Fangjun Jiang
on 17 Oct 2011
Believe it or not, you can use str2double directly.
a={'1','2'};
str2double(a)
Daniel Shub
on 17 Oct 2011
Wow, another reason to like str2double over str2num.
Jan
on 17 Oct 2011
cellfun(@str2double, C) is faster than str2double(C). Surprising!
Jan
on 17 Oct 2011
"cellfun(@str2double, C)" is faster than "str2double(C)". Surprising! But the indirection "cellfun(@(x)str2double(x), C)" wastes time.
Fangjun Jiang
on 17 Oct 2011
+1, For none-time-critical task, I still vote for using str2double().
hello_world
on 4 Jul 2018
Edited: Walter Roberson
on 13 Dec 2025
@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?
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!