Error using double - Conversion to double from cell is not possible.

조회 수: 8 (최근 30일)
David Garcia
David Garcia 2015년 10월 12일
답변: Image Analyst 2015년 10월 13일
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
dpb
dpb 2015년 10월 12일
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.

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

답변 (1개)

Image Analyst
Image Analyst 2015년 10월 13일
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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by