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

string cell arraywordcloud

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

Best Answer

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)