Borrar filtros
Borrar filtros

fill up a cell array with string elements

10 visualizaciones (últimos 30 días)
Ellen De Jonghe
Ellen De Jonghe el 13 de En. de 2020
Comentada: Fangjun Jiang el 26 de Feb. de 2020
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

Respuestas (2)

Fangjun Jiang
Fangjun Jiang el 13 de En. de 2020
This is all you need
words=split('this is a sentence.')
  3 comentarios
Fangjun Jiang
Fangjun Jiang el 14 de En. de 2020
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 el 26 de Feb. de 2020
or use cellstr(split("This is a sentence"))

Iniciar sesión para comentar.


Jonathan Allen
Jonathan Allen el 26 de Feb. de 2020
Replace last 2 lines with
[words(:)] = {k{:}};

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by