I need help adding a title to a Wordcloud

5 vues (au cours des 30 derniers jours)
Brian Leon
Brian Leon le 16 Avr 2020
Sorry for the extensive code, but I have a switch case that opens up a story depending on what you choose and it eventually makes a word cloud from the data. I am just unsure of how to make the title of my WordCloud be according to which story I choose. I have tried putting a title under every if statement but it does not work.
switch data
case 1 %case 1 for visualization
%creating the pop up window to choose stories
storyList = {'Peter Rabbit', 'Velveteen Rabbit', 'Peter Pan', 'The Snow Queen', 'Jack and the Bean Stalk'};
indx = listdlg('ListString', storyList, 'SelectionMode', 'Single');
%story options
if(indx==1)
fid = fopen('peterRabbit.txt');
ReadingData = textscan(fid,'%s');
title('Peter Rabbit')
elseif(indx==2)
fid = fopen('velveteenRabbit.txt');
ReadingData = textscan(fid,'%s');
title('Velveteen Rabbit')
elseif(indx==3)
fid = fopen('peterPan.txt');
ReadingData = textscan(fid,'%s');
title('Peter Pan')
elseif(indx==4)
fid = fopen('snowQueen.txt');
ReadingData = textscan(fid,'%s');
title('The Snow Queen')
elseif(indx==5)
fid = fopen('jackBeanStalk.txt');
ReadingData = textscan(fid,'%s');
title('Jack and the Bean Stalk')
end
%cleaning up the story files (CleanWord)
newWords = string();
for i = 1:length(ReadingData{1})
newWord = cleanWords(ReadingData{1}{i,1});
newWords = [newWords;newWord];
newWords = cellstr(newWords);
end
%removing the stopwords from the story files (isStopWord)
fid = fopen('stopWords.txt');
stopWords = textscan(fid,'%s');
stopWords = stopWords{1};
stopWord = isStopWord(stopWords,newWords);
w2 = newWords(cellfun(@ischar,newWords));
out = w2(~ismember(w2,stopWords));
%getting the unique words
numUniqueWords = getUniqueWords(out);
%getting the top ten words
topTenWords = getTopTenWords(numUniqueWords);
figure(1)
%creating the word cloud
subplot(1,2,1)
wordcloud({numUniqueWords.word},[numUniqueWords.frequency]);
end

Réponses (1)

Geoff Hayes
Geoff Hayes le 16 Avr 2020
Brian - you have the title already from your storyList and indx variables...so just re-use those just after you add the word cloud to the subplot.
switch data
case 1 %case 1 for visualization
%creating the pop up window to choose stories
storyList = {'Peter Rabbit', 'Velveteen Rabbit', 'Peter Pan', 'The Snow Queen', 'Jack and the Bean Stalk'};
indx = listdlg('ListString', storyList, 'SelectionMode', 'Single');
%story options
filename = '';
ReadingData = {};
if(indx==1)
filename = 'peterRabbit.txt';
elseif(indx==2)
filename = 'velveteenRabbit.txt';
elseif(indx==3)
filename = 'peterPan.txt';
elseif(indx==4)
filename = fopen('snowQueen.txt';
elseif(indx==5)
filename = 'jackBeanStalk.txt';
end
% read the file data
if ~isempty(filename)
fid = fopen(filename);
if fid > 0
ReadingData = textscan(fid,'%s');
fclose(fid); %<-------------- close the file!!
end
end
%cleaning up the story files (CleanWord)
newWords = string();
for i = 1:length(ReadingData{1})
newWord = cleanWords(ReadingData{1}{i,1});
newWords = [newWords;newWord];
newWords = cellstr(newWords);
end
%removing the stopwords from the story files (isStopWord)
fid = fopen('stopWords.txt');
stopWords = textscan(fid,'%s');
fclose(fid); %<------------- close the file!!
stopWords = stopWords{1};
stopWord = isStopWord(stopWords,newWords);
w2 = newWords(cellfun(@ischar,newWords));
out = w2(~ismember(w2,stopWords));
%getting the unique words
numUniqueWords = getUniqueWords(out);
%getting the top ten words
topTenWords = getTopTenWords(numUniqueWords);
figure(1)
%creating the word cloud
subplot(1,2,1)
wordcloud({numUniqueWords.word},[numUniqueWords.frequency]);
title(storyList{indx}) %<-------- set the title here
end
I've modified the if/elseif bodies to reduce duplicated code. Also, if you open a file with fopen then you need to close it afterwards with fclose.

Catégories

En savoir plus sur Data Distribution Plots 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!

Translated by