how to count words in a cell array

6 次查看(过去 30 天)
Alexya
Alexya 2022-10-26
回答: Matt J 2022-10-26
I have a cell array that i want to count the words in the strings inside the cell array
this is what i have so far. I need a general direction on where to go from here.
function [words] = howManyWords(ca)
i = 1
ca = {{'Let it go'} {'Let it go'} { 'Can''t hold it back anymore'}}
stringbaby = string(ca)
howmanystrings = length(stringbaby)
for i = 1:howmanystrings
n = strfind(ca(i),' ')
words(i) = length(n) + 1
i = i + 1
end
end
  1 个评论
Matt J
Matt J 2022-10-26
I need a general direction on where to go from here
Why do you need to go anywhere?

请先登录,再进行评论。

回答(2 个)

David Hill
David Hill 2022-10-26
ca = {{'Let it go'} {'Let it go'} { 'Can''t hold it back anymore'}}
ca = 1×3 cell array
{1×1 cell} {1×1 cell} {1×1 cell}
for i = 1:length(ca)
a=ca{i};
a=strsplit(a{1},' ');
words(i)=length(a)';
end
words
words = 1×3
3 3 5

Matt J
Matt J 2022-10-26
ca = {{'Let it go'} {'Let it go'} { 'Can''t hold it back anymore'}};
NumWords = cellfun(@(c)sum(c{1}==' ')+1,ca)
NumWords = 1×3
3 3 5

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by