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

조회 수: 3 (최근 30일)
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?

채택된 답변

dpb
dpb 2013년 11월 18일
편집: dpb 2013년 11월 18일
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
Michael
Michael 2013년 11월 18일
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 2013년 11월 18일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by