The following error occurred converting from cell to double:

1 view (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 1 May 2013
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);

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!