Trying to write a function with random, input and fprint

I am trying to write a function that asks for names, for example it should first ask, What are the names of the players. Then is should pick one name with rand and decide if the player should play goalkeeper, defencer, midfielder og striker. Then it should print out for example... David is going to play as a striker. I have tried everything but i dont get it to work!

2 Comments

You should show an example of what you have tried
Nafn={'Arnar' 'Hlynur' 'Ingimar' 'Stebbi'};
Drykkur={'Bjór' 'Skot' '2 sopar' 'Ekkert' '1 sopi' 'Hálfur bjór'};
v = randi(length(Nafn));
f = Drykkur(randi(length(Drykkur)));
fprintf('Meistari %s á að drekka %s og vera glæsilegur!\n', v, f);
This is one version of what i have tried

Sign in to comment.

 Accepted Answer

Try it this way:
Nafn={'Arnar' 'Hlynur' 'Ingimar' 'Stebbi'};
Drykkur={'Bjór' 'Skot' '2 sopar' 'Ekkert' '1 sopi' 'Hálfur bjór'};
button = menu('Enter a player name', Nafn);
message = sprintf('%s is a %s', Nafn{button}, Drykkur{button});
uiwait(msgbox(message));

4 Comments

This is in the right direction but not 100% I also want to randomise the "Nafn".... :)
@Hlynur: Do you mean something like this:
Nafn = Nafn(randperm(4))
?
Try it this way:
Drykkur={'Bjór' 'Skot' '2 sopar' 'Ekkert' '1 sopi' 'Hálfur bjór'};
numberOfPositions = length(Drykkur);
for p = 1 : numberOfPositions
% Ask user for a name.
titleBar = 'Enter a player name';
userPrompt = 'Enter a player name';
ca = inputdlg(userPrompt, titleBar, 1);
if isempty(ca)
numberOfPositions = p-1;
break;
end
Nafn(p) = ca;
end
% Randomize the names of the players.
Nafn = Nafn(randperm(numberOfPositions))
message = '';
for p = 1 : numberOfPositions
message = sprintf('%s\n%s is a %s', ...
message, Nafn{p}, Drykkur{p});
end
uiwait(msgbox(message));

Sign in to comment.

More Answers (0)

Categories

Find more on View and Analyze Simulation Results 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!