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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by