我正在研究这个matlab代码,它是从文本文档中读取内容并将单词存储到数组中并查找每个单词的长度。以下是我的代码:
file1=fopen('doc1.txt','r');
%file 1 is now open
%read data from file 1
text1=fileread('doc1.txt');
%now text1 has the content of doc1 as a string.Next split the sentences
%into words.For that we are calling the split function
temp1=strsplit(text1,' ');
[r,c]=size(temp1);
disp('The total number of distinct words in the document are ')
c
disp('And those words are :')
for i=1:c
k= temp1(i)
length(k)
end
这里无论每个单词的长度是多少,长度(k)总是显示1.有人可以帮我解决这个问题吗?先谢谢。
答案 0 :(得分:2)
temp1
是一个cell
数组。您应该使用大括号索引来提取单个字符串,如此
words = 'foo bar1 baz23';
temp1 = strsplit(words, ' ');
for i = 1:numel(temp1)
k = temp1{i}
length(k)
end