Clear Filters
Clear Filters

how to store function [x] = input('blah')

4 views (last 30 days)
GOENG on 16 Apr 2015
Commented: Michael Haderlein on 16 Apr 2015
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()
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();
disp('Right, you got it. Thank you for playing.');
gameOver = true;
function [x] = inPut()
[x] = input('Please enter a number between 1 and 100: ');
I still got lots to learn, please help. Thank you in advance :D

Accepted Answer

pfb on 16 Apr 2015
As far as I understand you want that the input variable to be a number called guess. For your usage of the input function that should be simply
guess = x;
But why don't you simplify it into
guess = input('Please enter a number between 1 and 100: ');
The function inPut seems superfluous.
  1 Comment
GOENG on 16 Apr 2015
yea I was over complicating things when it was just a simple change. Figured it out as soon as the post went up haha. And wasn't sure how to put this post down since it's my first time =_=

Sign in to comment.

More Answers (1)

Michael Haderlein
Michael Haderlein on 16 Apr 2015
You mean, like this?
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.
GOENG on 16 Apr 2015
yea pretty much, it was simple but didn't really think about it too much haha. And I'm trying to write this without using nested so that's why I have it out because I needed to meet some conditions.
On that note, would you happen to know how to call a variable from another function? eg
function y = test1()
x = 5;
function x = test2()
%i want to recall x here;
xyz = x+1;
something like that
Michael Haderlein
Michael Haderlein on 16 Apr 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
There are some more options considering the scope of variables, but this is typically quite confusing especially for beginners.

Sign in to comment.


Find more on Conway's Game of Life 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!