how to store function [x] = input('blah')
Mostrar comentarios más antiguos
Hello, I'm trying to store my input into another variable while being called from another function.
function hiloTest()
gameOver = 0;
while gameOver == 0
[gameOver, guess] = showResults()
end
end
function [gameOver, guess] = showResults()
gameOver = false;
ii = 46;
%I want to store this [x] = inPut() function's input into variable named 'guess'
[x] = inPut()
while (gameOver == 0)
if (guess < ii)
disp('Close, but you need to go a bit higher.');
[x] = inPut();
elseif (guess > ii)
disp('Your guess is high. Please try again.');
[x] = inPut();
else
disp('Right, you got it. Thank you for playing.');
gameOver = true;
end
end
end
function [x] = inPut()
[x] = input('Please enter a number between 1 and 100: ');
end
I still got lots to learn, please help. Thank you in advance :D
Respuesta aceptada
Más respuestas (1)
Michael Haderlein
el 16 de Abr. de 2015
You mean, like this?
guess=inPut;
Btw, your first while loop (the one outside showResults) is not necessary. You loop inside your nested function and the nested function will always return false as value for gameOver. Thus, the loop will be iterated exactly once.
2 comentarios
GOENG
el 16 de Abr. de 2015
Michael Haderlein
el 16 de Abr. de 2015
Right, there's no nested function, my bad. Still, the one loop is just not looping and therefore not necessary.
Your question now is not quite related to the original one, better post new questions then. Anyway, to make it short: Best solution is to make x a second output argument of test1():
function [y,x]=test1
and also an input argument of test2.
function xyz=test2(x)
In your main function, you'll then write something like
[Y,X]=test1;
XYZ=test2(X);
There are some more options considering the scope of variables, but this is typically quite confusing especially for beginners.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!