Error using double - Conversion to double from cell is not possible.
5 views (last 30 days)
Show older comments
Greetings,
I got this problem:
"The following error occurred converting from cell to double:
Error using double
Conversion to double from cell is not possible.
Error in marksforUnisens (line 34)
Y(i,:)=[zz(i),b(i)];
Could you please help me to convert from cell to double when b contains text (is char)? Thank you in advance!
filename1= (answer{2});
A=importdata(filename1);
D=A.textdata;
a=D(:,9);
aa= cellfun(@str2num, {a{:}}, 'Uniform', 0)';
sample=str2num(answer{1});
Z=zeros(length(aa),1);
for i=2:length(aa)
z(i)=[aa{i}*sample];
end
zz=z';
b=D(:,7);
Y=zeros(length(aa),2);
for i=1:length(aa)
Y(i,:)=[zz(i),b(i)];
end
1 Comment
dpb
on 12 Oct 2015
It doesn't make any sense to convert text to double(). What is in b? If it's more numeric data similar to that in D(:,9) I'm guessing, you've already shown how to convert it earlier.
But, if that's the case, make life easier for yourself and if you're going to use importdata, for the numeric data use the A.data structure field and it will already be double.
If that causes a problem, show a short snippet of the data file; maybe there's a better way to read it.
Answers (1)
Image Analyst
on 13 Oct 2015
If the badly-named b contains text, then it's most likely a cell array, and that's also what the error message indicates. I'm guessing z is numbers and if you're going to want to append b to z, then I'm guessing b is text strings of numerals. So you'd need to get the string out of the cell, then call str2double:
Y(i,:) = [zz(i), str2double(b{i})];
b(i) is a cell, whereas b{i} is the contents of the cell, which is a string. Please read the FAQ so that you will, from now on, have a good intuitive feel for how to use cell arrays and when to use braces and when to use parentheses: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
0 Comments
See Also
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!