MATLAB Answers

kenny
0

randomly displaying a string from an array

Asked by kenny
on 20 Jun 2013

hi! I was hoping to randomly display one of the following four greetings each iteration, but when i run it, it says that I can't use "any". and when i try "rand" a similar problem occurs where the input must be numeric- how would i do this with strings?

thank you

 ??? Undefined function or method 'any' for input arguments of type 'cell'.

code:

 X = {'hello buddy', 'hi there', 'good morning', 'top o' the morning'}
 disp(any(X))

  0 Comments

Tags

Products

No products are associated with this question.

1 Answer

Answer by Andrei Bobrov
on 20 Jun 2013
 Accepted answer

out = X(randi(numel(X)));

  1 Comment

be sure to use the new rng() function, and seed it in a non-predictable way.

Ref: http://www.mathworks.com/help/matlab/ref/rng.html?searchHighlight=rng


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!