choose a random text in matlab

To choose a random text in MATLAB, you need to have a cell array of text strings. Here’s an example:

main.m
myText = {'Hello', 'world', 'how', 'are', 'you'};
50 chars
2 lines

Now, to choose a random text from this list, you can use the randi() function to generate a random index within the range of the cell array and use it to access a text string at that position like this:

main.m
index = randi([1,length(myText)]);
randomText = myText{index};
disp(randomText);
81 chars
4 lines

This will print a random text to the console. You can modify the range of randi() function to choose a random text from a particular range only.

main.m
index = randi([2,4]);
randomText = myText{index};
disp(randomText);
68 chars
4 lines

This modification will always print a random text from {'world', 'how', 'are'}.

related categories

gistlibby LogSnag