obtaining random data in cell array
Show older comments
in the attachment i have imported an excel data sheet containing a column of 100 english nouns. i then imported the data interactively and can be found in the workspace as a cell array with the value of 100x1, named 'nouns'. i am just wondering how i can generate a random integer between 1 and 100 that can choose any one of these words from the data (in short, randomly picking any word from the list of nouns in the workspace).
eventually the objective is to create a hangman game displayed in the image

thanks!
2 Comments
Not enough information. What does "i have a 100x1 cell made up of 100 words in each cell" really mean? Are the words stored in a string, or a char vector as natural language sentences, or a char matrix with one word per row padded with spaces, or a cell array of individual words, or something else?
Rather than trying to explain how the data is arranged, it would be much simpler if you simply uploaded the data for us to look at. Or something that is similar enough and has the same features as your data.
Simply edit your question and click the paperclip button to upload the data.
Brigham Tukukino
on 27 Oct 2017
Answers (3)
KSSV
on 26 Oct 2017
randperm(100,1)
Jan
on 26 Oct 2017
randi([1,100], 1)
or simpler
randi(100)
Image Analyst
on 27 Oct 2017
Try this, which is robust as to the number of words in your workbook
% Read words from the Excel file.
[~, words] = xlsread(xlfileName);
% Get the index of a random word to use. Does not assume 100 - can be any length!
randomIndex = randi(size(words, 1));
% Get the actual word (extract it into a string from the cell).
wordToUse = strings{randomIndex};
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!