MATLAB Answers

fill up a cell array with string elements

8 views (last 30 days)
Ellen De Jonghe
Ellen De Jonghe on 13 Jan 2020
Commented: Fangjun Jiang on 14 Jan 2020
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,:);


Sign in to comment.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 13 Jan 2020
This is all you need
words=split('this is a sentence.')


Ellen De Jonghe
Ellen De Jonghe on 14 Jan 2020
If I do that, I think i will get a string array. But I need the seperate words in a cell array.
Fangjun Jiang
Fangjun Jiang on 14 Jan 2020
It depends on the data type of the input. In your case, you can add one more line to convert it

Sign in to comment.

Sign in to answer this question.

Translated by