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 Kommentare

You should show an example of what you have tried
Hlynur
Hlynur am 6 Dez. 2012
Bearbeitet: Hlynur am 6 Dez. 2012
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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 6 Dez. 2012
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 Kommentare

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));
Thanks alot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu View and Analyze Simulation Results finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by