Converting a vertical text file into a character array for use in a Word Cloud

Illustration
Aidan Lyons - 2022-04-09T12:14:35+00:00
Question: Converting a vertical text file into a character array for use in a Word Cloud

Im trying to read in this file so that it becomes a cell array of strings, so that I can combine with a cell array of corresponding occurances into a table for use in the wordcloud function. I can't seem to figure out how to read it in and manipulate it so that each word in the list is a string like a normal string cell array. Any suggestions? thanks

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

filename = 'YourFile.txt'
S = regexp( fileread(filename), '\r?\n', 'split');
S(cellfun(@isempty,S)) = [];    %remove empty lines, especially at the end

Now S will be a cell array of character vectors. It will not be a cell array of string objects because MATLAB uses either cell array of character vectors, or else uses string() arrays. You can convert it to a string array by using string(S)


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!