Printing a variable within an input command

32 visualizaciones (últimos 30 días)
Chris Emmett
Chris Emmett el 11 de Nov. de 2019
Respondida: Image Analyst el 11 de Nov. de 2019
I am trying to use an input command to tell a specific player that it is there turn to make a move.
My code looks like this:
move = input(['Player ' current_player ', choose column 1-7 to drop a chip: ']);
The output looks like this:
Player , choose column 1-7 to drop a chip:
Can someone help me get this working correctly?

Respuestas (2)

Star Strider
Star Strider el 11 de Nov. de 2019
Assuming ‘current_player’ is a character array or string variable:
move = input(sprintf('Player %s, choose column 1-7 to drop a chip: ', current_player));
Otherwise, choose the appropriate format descriptor in formatSpec for the ‘current_player’ variable class.

Image Analyst
Image Analyst el 11 de Nov. de 2019
I think current_player is an integer, and that's why when you create a string like this:
['Player ' current_player ', choose column 1-7 to drop a chip: ']
it's making non-printable characters. Like if current_player was some number less than ASCII 32. You need to use %d or num2str
move = input(sprintf('Player %s, choose column 1-7 to drop a chip: ', current_player));
Alternatively if you don't like sprintf() for some reason, you can use brackets, quotes, and num2str() to create a character array
userPrompt = ['Player ', num2str(current_player), ' choose column 1-7 to drop a chip: '];
move = input(userPrompt);

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by