How to have the length of the words in each line
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I am trying to write a function that gives me a matrix which shows the number of characters for each word of a textfile. for example if my text file is lyric = 'the cat is running on the wall', the answer shuld = [3 2 7 2 3 4] In order to do so, the function should a text file as an input and by using textscan in a for-loop format, it goes through the text and stops whenever it reaches the space and then it gives the number of characters from the first word to the last word. I appreciate if you can help me do this. Thank You,
2 commentaires
Guillaume
le 9 Nov 2017
Modifié(e) : Guillaume
le 10 Nov 2017
Do not edit your question away or delete comments that are useful to others for understanding the answer given. Answers are not just for your benefit.
Editing your question away angers the people who spend time helping and ensure they won't answer your question anymore.
I will ask mathworks to restore the question to its original state.
For information, the original question was how to use textscan to count the number of words in a text file.
Réponse acceptée
Guillaume
le 9 Nov 2017
Modifié(e) : Guillaume
le 9 Nov 2017
I wouldn't use textscan for that. Probably the easiest would be:
[wordstarts, wordends] = regexp(yourtext, '\w+');
wordlengths = wordends - wordstarts + 1;
The advantage of the above is that it is unicode compatible. Anything that's not consider a letter by the unicode standard is a separator for your word count.
1 commentaire
Guillaume
le 9 Nov 2017
Use fileread to read the whole file in one go.
[wordstarts, wordends] = regexp(fileread(somefile), '\w+');
wordlengths = wordends - wordstarts + 1;
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Language Support dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!