The following error occurred converting from cell to double:

1 次查看(过去 30 天)
hi i am very new to matlab so this might sound stupid to the experts so i appoligize
i need to create a function the gets a cell of words and a number and returns a new cell only containing word equal or longer to the number for exaample ({'is','a','sentence'},2)should come out is sentence now this is what i did
function newWordsList=eraseShortWords(worldlist,n)
counter=0;
%get the number of words
k=length(worldlist);
chosen=zeros(1,k);
for i=1:k
l=length(cell2mat(worldlist(1,i)));
if l>~n
counter=counter+1;
chosen(i)=worldlist(i);
end
newWordsList=chosen;
end
i keep getting error eraseShortWords({'add','dddd'},3) The following error occurred converting from cell to double: Error using double Conversion to double from cell is not possibl

回答(1 个)

Walter Roberson
Walter Roberson 2013-5-1
You initialize chosen=zeros(1,k) so chosen is numeric. But you have
chosen(i)=wordlist(i)
and wordlist(i) is a cell array. You cannot store a cell array into a numeric location.
Likely fix:
chosen = cell(1,k);

类别

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