I want to write a function that allows me to input a certain number of guesses depending on the dimensions

3 views (last 30 days)
I want my function to look something like function[output] = guess(f,N) f will be an anonymous function and N will be the number of dimensions. If N is equal to 2 i want to make 3 initial guesses, if N is 3 I want four guesses and so on. for example if i input "guess(f,2)" Matlab would than ask me for guess1, than guess2, and than guess3.
Is it possible to do something like this within Matlab?

Accepted Answer

dpb
dpb on 18 Nov 2013
Edited: dpb on 18 Nov 2013
function g=guess(f,N)
% returns N+1 values as inputs from user
g=zeros(N+1,1);
for i=1:N+1
g(i)=input(['Enter guess ' num2str(i) ': '])
end
It's not at all clear what role (if any) the function input is to play here.
You'll want to add error handling, etc., and the input can be "prettified" with use of inputdlg or other uitools as desired depending on just how fancy you want it to be.
  2 Comments
Michael
Michael on 18 Nov 2013
Thank you. Is there anyway i can input the guess as a point? ex. (2,2) or (2,2,2) this would depend on the dimensions, N.
dpb
dpb on 18 Nov 2013
Well, you can do anything you want to, in general...not positive what you really want here, but you can make something look like it. Forcing the user to actually do it is a little more tricky, however.
>> N=2;
>> prmpt=sprintf(['Enter %d terms as (' repmat(' , ',1,N) '): '],N+1);
>> s=input(prmpt,'s')
Enter 3 terms as ( , , ): (2,2,2)
s =
(2,2,2)
>>
I followed the rules w/ my input as you see. And, of course, what you have here is a character string.
If you try this w/o the 's' optional argument to input(), havoc ensues--
>> s=input(prmpt)
Enter 3 terms as ( , , ): (2,2,2)
(2,2,2)
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
>>
The question here would be what are you really trying to do?

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!