read in text file and place each word of the sentence it's own separate cell

I have a text file with a bunch of sentences. There is one sentence per line and I would like read each sentence in and place it in a cell array so that each word of each sentence is in its own separate cell. I am guessing that I would use strsplit?
Right now I have a .mat file where each sentence is in its own cell but I want to further delimit them so that every word is in it's own cell. If I have 10 four word sentences, I would like to have a 1x10 cell array so that when I click on each cell, the sentence is separated into four cells.

 Akzeptierte Antwort

Marc Jakobi
Marc Jakobi am 11 Okt. 2016
Bearbeitet: Marc Jakobi am 11 Okt. 2016
If you already have a cell array C with the separated sentences:
D = cell(1, length(C));
for i = 1:length(C)
c = strsplit(regexprep(C{i}, {',', ';', ',', ':'}, {'', '', '', ''}), ' ');
D(i,1:length(c)) = c;
end
Note: You may have to add more symbols to the rexexprep arguments.

3 Kommentare

addone=1
for i=1:10 %number of sentences in the .mat file
C=strsplit(sent_all{1,addone}) %sent_all is the list of sentences
addone=addone+1
%%%%%I don't know what goes here. I want to print every sentence in a separate cell like you did in your code. Ultimately, I would like a 1x10 array. Within each cell, one sentence will be broken up into separate cells.%%%%%% end
So like this?
C = cell(1, 10);
for i = 1:10
C{i} = strsplit(sent_all{i}); %note the difference between curly brackets and regular ones for cell arrays
end
That is exactly what I needed! Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 11 Okt. 2016

Kommentiert:

am 11 Okt. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by