fill up a cell array with string elements

Hye
I have to write a function that puts the words of a string in a cell array.
I've made the following script.
I already get a cell array with the right amount of cells, but they are all filled up with the first word of the string.
How do I get this right?
function [words] = giveWords(sentence)
k = split(sentence);
words = cell(1,length(k));
words(1,:) = k(1,:);
end

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2020년 1월 13일

0 개 추천

This is all you need
words=split('this is a sentence.')

댓글 수: 3

If I do that, I think i will get a string array. But I need the seperate words in a cell array.
82469426_751600275361231_7318586974888001536_n.png
It depends on the data type of the input. In your case, you can add one more line to convert it
words=mat2cell(names,repmat(1,1,size(names,1)),repmat(1,size(names,2),1))
Fangjun Jiang
Fangjun Jiang 2020년 2월 26일
or use cellstr(split("This is a sentence"))

댓글을 달려면 로그인하십시오.

Jonathan Allen
Jonathan Allen 2020년 2월 26일

0 개 추천

Replace last 2 lines with
[words(:)] = {k{:}};

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2020년 1월 13일

댓글:

2020년 2월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by