obtaining random data in cell array

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

Stephen23
Stephen23 on 26 Oct 2017
Edited: Stephen23 on 26 Oct 2017
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.
thanks stephen, i have updated the question. look forward to your reply

Sign in to comment.

Answers (3)

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

Asked:

on 26 Oct 2017

Answered:

on 27 Oct 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!